]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
fixed fl makefiles that Julian didn't fix correctly
[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 #ifdef __GNUG__
11 #pragma implementation "textctrl.h"
12 #endif
13
14 #include "wx/textctrl.h"
15 #include "wx/utils.h"
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #include "wx/settings.h"
19 #include "wx/panel.h"
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <ctype.h>
24 #include <math.h> // for fabs
25
26 #include "gdk/gdk.h"
27 #include "gtk/gtk.h"
28 #include "gdk/gdkkeysyms.h"
29
30 //-----------------------------------------------------------------------------
31 // idle system
32 //-----------------------------------------------------------------------------
33
34 extern void wxapp_install_idle_handler();
35 extern bool g_isIdle;
36
37 //-----------------------------------------------------------------------------
38 // data
39 //-----------------------------------------------------------------------------
40
41 extern bool g_blockEventsOnDrag;
42 extern wxCursor g_globalCursor;
43
44 //-----------------------------------------------------------------------------
45 // "changed"
46 //-----------------------------------------------------------------------------
47
48 static void
49 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
50 {
51 if (!win->m_hasVMT) return;
52
53 if (g_isIdle)
54 wxapp_install_idle_handler();
55
56 win->SetModified();
57 win->UpdateFontIfNeeded();
58
59 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
60 event.SetString( win->GetValue() );
61 event.SetEventObject( win );
62 win->GetEventHandler()->ProcessEvent( event );
63 }
64
65 //-----------------------------------------------------------------------------
66 // "changed" from vertical scrollbar
67 //-----------------------------------------------------------------------------
68
69 static void
70 gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
71 {
72 if (!win->m_hasVMT) return;
73
74 if (g_isIdle)
75 wxapp_install_idle_handler();
76
77 win->CalculateScrollbar();
78 }
79
80 //-----------------------------------------------------------------------------
81 // "focus_in_event"
82 //-----------------------------------------------------------------------------
83
84 extern wxWindow *g_focusWindow;
85 extern bool g_blockEventsOnDrag;
86 // extern bool g_isIdle;
87
88 static gint gtk_text_focus_in_callback( GtkWidget *widget, GdkEvent *WXUNUSED(event), wxWindow *win )
89 {
90 // Necessary?
91 #if 0
92 if (g_isIdle)
93 wxapp_install_idle_handler();
94 #endif
95 if (!win->m_hasVMT) return FALSE;
96 if (g_blockEventsOnDrag) return FALSE;
97
98 g_focusWindow = win;
99
100 // notify the parent that we got the focus
101 wxChildFocusEvent eventFocus(win);
102 (void)win->GetEventHandler()->ProcessEvent(eventFocus);
103
104 #ifdef HAVE_XIM
105 if (win->m_ic)
106 gdk_im_begin(win->m_ic, win->m_wxwindow->window);
107 #endif
108
109 #if 0
110 #ifdef wxUSE_CARET
111 // caret needs to be informed about focus change
112 wxCaret *caret = win->GetCaret();
113 if ( caret )
114 {
115 caret->OnSetFocus();
116 }
117 #endif // wxUSE_CARET
118 #endif
119
120 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
121 event.SetEventObject( win );
122
123 if (win->GetEventHandler()->ProcessEvent( event ))
124 {
125 return TRUE;
126 }
127
128 return FALSE;
129 }
130
131 //-----------------------------------------------------------------------------
132 // "focus_out_event"
133 //-----------------------------------------------------------------------------
134
135 static gint gtk_text_focus_out_callback( GtkWidget *widget, GdkEvent *WXUNUSED(event), wxWindow *win )
136 {
137 #if 0
138 if (g_isIdle)
139 wxapp_install_idle_handler();
140 #endif
141
142 if (!win->m_hasVMT) return FALSE;
143 if (g_blockEventsOnDrag) return FALSE;
144
145 #if 0
146 // if the focus goes out of our app alltogether, OnIdle() will send
147 // wxActivateEvent, otherwise gtk_window_focus_in_callback() will reset
148 // g_sendActivateEvent to -1
149 g_sendActivateEvent = 0;
150 #endif
151
152 wxWindow *winFocus = wxFindFocusedChild(win);
153 if ( winFocus )
154 win = winFocus;
155
156 g_focusWindow = (wxWindow *)NULL;
157
158 #ifdef HAVE_XIM
159 if (win->m_ic)
160 gdk_im_end();
161 #endif
162
163 #if 0
164 #ifdef wxUSE_CARET
165 // caret needs to be informed about focus change
166 wxCaret *caret = win->GetCaret();
167 if ( caret )
168 {
169 caret->OnKillFocus();
170 }
171 #endif // wxUSE_CARET
172 #endif
173
174 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
175 event.SetEventObject( win );
176
177 if (win->GetEventHandler()->ProcessEvent( event ))
178 {
179 return TRUE;
180 }
181
182 return FALSE;
183 }
184
185 //-----------------------------------------------------------------------------
186 // wxTextCtrl
187 //-----------------------------------------------------------------------------
188
189 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
190
191 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
192 EVT_CHAR(wxTextCtrl::OnChar)
193
194 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
195 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
196 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
197 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
198 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
199
200 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
201 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
202 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
203 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
204 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
205 END_EVENT_TABLE()
206
207 void wxTextCtrl::Init()
208 {
209 m_modified = FALSE;
210 m_updateFont = FALSE;
211 m_text =
212 m_vScrollbar = (GtkWidget *)NULL;
213 }
214
215 wxTextCtrl::wxTextCtrl( wxWindow *parent,
216 wxWindowID id,
217 const wxString &value,
218 const wxPoint &pos,
219 const wxSize &size,
220 long style,
221 const wxValidator& validator,
222 const wxString &name )
223 {
224 Init();
225
226 Create( parent, id, value, pos, size, style, validator, name );
227 }
228
229 bool wxTextCtrl::Create( wxWindow *parent,
230 wxWindowID id,
231 const wxString &value,
232 const wxPoint &pos,
233 const wxSize &size,
234 long style,
235 const wxValidator& validator,
236 const wxString &name )
237 {
238 m_needParent = TRUE;
239 m_acceptsFocus = TRUE;
240
241 if (!PreCreation( parent, pos, size ) ||
242 !CreateBase( parent, id, pos, size, style, validator, name ))
243 {
244 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
245 return FALSE;
246 }
247
248
249 m_vScrollbarVisible = FALSE;
250
251 bool multi_line = (style & wxTE_MULTILINE) != 0;
252 if (multi_line)
253 {
254 #if (GTK_MINOR_VERSION > 2)
255 /* a multi-line edit control: create a vertical scrollbar by default and
256 horizontal if requested */
257 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
258 #else
259 bool bHasHScrollbar = FALSE;
260 #endif
261
262 /* create our control ... */
263 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
264
265 /* ... and put into the upper left hand corner of the table */
266 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
267 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
268 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
269 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
270 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
271 0, 0);
272
273 /* always wrap words */
274 gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE );
275
276 #if (GTK_MINOR_VERSION > 2)
277 /* put the horizontal scrollbar in the lower left hand corner */
278 if (bHasHScrollbar)
279 {
280 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
281 GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS );
282 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
283 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
284 GTK_FILL,
285 0, 0);
286 gtk_widget_show(hscrollbar);
287
288 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
289 gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE );
290 }
291 #endif
292
293 /* finally, put the vertical scrollbar in the upper right corner */
294 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
295 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS );
296 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
297 GTK_FILL,
298 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
299 0, 0);
300 }
301 else
302 {
303 /* a single-line text control: no need for scrollbars */
304 m_widget =
305 m_text = gtk_entry_new();
306 }
307
308 m_parent->DoAddChild( this );
309
310 PostCreation();
311
312 SetFont( parent->GetFont() );
313
314 wxSize size_best( DoGetBestSize() );
315 wxSize new_size( size );
316 if (new_size.x == -1)
317 new_size.x = size_best.x;
318 if (new_size.y == -1)
319 new_size.y = size_best.y;
320 if ((new_size.x != size.x) || (new_size.y != size.y))
321 SetSize( new_size.x, new_size.y );
322
323 if (multi_line)
324 gtk_widget_show(m_text);
325
326 if (multi_line)
327 {
328 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed",
329 (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this );
330
331 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text)), "focus_in_event",
332 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback), (gpointer)this );
333
334 gtk_signal_connect( GTK_OBJECT(GTK_TEXT(m_text)), "focus_out_event",
335 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback), (gpointer)this );
336 }
337 else
338 {
339 gtk_signal_connect( GTK_OBJECT(m_text), "focus_in_event",
340 GTK_SIGNAL_FUNC(gtk_text_focus_in_callback), (gpointer)this );
341
342 gtk_signal_connect( GTK_OBJECT(m_text), "focus_out_event",
343 GTK_SIGNAL_FUNC(gtk_text_focus_out_callback), (gpointer)this );
344 }
345
346 if (!value.IsEmpty())
347 {
348 gint tmp = 0;
349
350 #if GTK_MINOR_VERSION == 0
351 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
352 // gtk_editable_insert_text()
353 gtk_widget_realize(m_text);
354 #endif // GTK 1.0
355
356 #if wxUSE_UNICODE
357 wxWX2MBbuf val = value.mbc_str();
358 gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp );
359 #else // !Unicode
360 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
361 #endif // Unicode/!Unicode
362
363 if (multi_line)
364 {
365 /* bring editable's cursor uptodate. bug in GTK. */
366
367 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
368 }
369 }
370
371 if (style & wxTE_PASSWORD)
372 {
373 if (!multi_line)
374 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
375 }
376
377 if (style & wxTE_READONLY)
378 {
379 if (!multi_line)
380 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
381 }
382 else
383 {
384 if (multi_line)
385 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
386 }
387
388 /* we want to be notified about text changes */
389 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
390 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
391
392 /* we don't set a valid background colour, because the window
393 manager should use a default one */
394 m_backgroundColour = wxColour();
395
396 wxColour colFg = parent->GetForegroundColour();
397 SetForegroundColour( colFg );
398
399 m_cursor = wxCursor( wxCURSOR_IBEAM );
400
401 // FIXME: is the bg colour correct here?
402 wxTextAttr attrDef( colFg,
403 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW),
404 parent->GetFont() );
405 SetDefaultStyle( attrDef );
406
407 Show( TRUE );
408
409 return TRUE;
410 }
411
412 void wxTextCtrl::CalculateScrollbar()
413 {
414 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
415
416 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
417
418 if (adj->upper - adj->page_size < 0.8)
419 {
420 if (m_vScrollbarVisible)
421 {
422 gtk_widget_hide( m_vScrollbar );
423 m_vScrollbarVisible = FALSE;
424 }
425 }
426 else
427 {
428 if (!m_vScrollbarVisible)
429 {
430 gtk_widget_show( m_vScrollbar );
431 m_vScrollbarVisible = TRUE;
432 }
433 }
434 }
435
436 wxString wxTextCtrl::GetValue() const
437 {
438 wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") );
439
440 wxString tmp;
441 if (m_windowStyle & wxTE_MULTILINE)
442 {
443 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
444 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
445 tmp = wxString(text,*wxConvCurrent);
446 g_free( text );
447 }
448 else
449 {
450 tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent);
451 }
452 return tmp;
453 }
454
455 void wxTextCtrl::SetValue( const wxString &value )
456 {
457 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
458
459 if (m_windowStyle & wxTE_MULTILINE)
460 {
461 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
462 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
463 len = 0;
464 #if wxUSE_UNICODE
465 wxWX2MBbuf tmpbuf = value.mbc_str();
466 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len );
467 #else
468 gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len );
469 #endif
470 }
471 else
472 {
473 gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() );
474 }
475
476 // GRG, Jun/2000: Changed this after a lot of discussion in
477 // the lists. wxWindows 2.2 will have a set of flags to
478 // customize this behaviour.
479 SetInsertionPoint(0);
480
481 m_modified = FALSE;
482 }
483
484 void wxTextCtrl::WriteText( const wxString &text )
485 {
486 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
487
488 if ( text.empty() )
489 return;
490
491 #if wxUSE_UNICODE
492 wxWX2MBbuf buf = text.mbc_str();
493 const char *txt = buf;
494 size_t txtlen = strlen(buf);
495 #else
496 const char *txt = text;
497 size_t txtlen = text.length();
498 #endif
499
500 if ( m_windowStyle & wxTE_MULTILINE )
501 {
502 /* this moves the cursor pos to behind the inserted text */
503 gint len = GTK_EDITABLE(m_text)->current_pos;
504
505 // if we have any special style, use it
506 if ( !m_defaultStyle.IsDefault() )
507 {
508 GdkFont *font = m_defaultStyle.HasFont()
509 ? m_defaultStyle.GetFont().GetInternalFont()
510 : NULL;
511
512 GdkColor *colFg = m_defaultStyle.HasTextColour()
513 ? m_defaultStyle.GetTextColour().GetColor()
514 : NULL;
515
516 GdkColor *colBg = m_defaultStyle.HasBackgroundColour()
517 ? m_defaultStyle.GetBackgroundColour().GetColor()
518 : NULL;
519
520 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
521 }
522 else // no style
523 {
524 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
525 }
526
527 /* bring editable's cursor uptodate. bug in GTK. */
528 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
529 }
530 else // single line
531 {
532 /* this moves the cursor pos to behind the inserted text */
533 gint len = GTK_EDITABLE(m_text)->current_pos;
534 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
535
536 /* bring editable's cursor uptodate. bug in GTK. */
537 GTK_EDITABLE(m_text)->current_pos += text.Len();
538
539 /* bring entry's cursor uptodate. bug in GTK. */
540 gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos );
541 }
542 }
543
544 void wxTextCtrl::AppendText( const wxString &text )
545 {
546 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
547
548 if ( text.empty() )
549 return;
550
551 #if wxUSE_UNICODE
552 wxWX2MBbuf buf = text.mbc_str();
553 const char *txt = buf;
554 size_t txtlen = strlen(buf);
555 #else
556 const char *txt = text;
557 size_t txtlen = text.length();
558 #endif
559
560 if (m_windowStyle & wxTE_MULTILINE)
561 {
562 if ( !m_defaultStyle.IsDefault() )
563 {
564 wxFont font = m_defaultStyle.HasFont() ? m_defaultStyle.GetFont()
565 : m_font;
566 GdkFont *fnt = font.Ok() ? font.GetInternalFont() : NULL;
567
568 wxColour col = m_defaultStyle.HasTextColour()
569 ? m_defaultStyle.GetTextColour()
570 : m_foregroundColour;
571 GdkColor *colFg = col.Ok() ? col.GetColor() : NULL;
572
573 col = m_defaultStyle.HasBackgroundColour()
574 ? m_defaultStyle.GetBackgroundColour()
575 : m_backgroundColour;
576 GdkColor *colBg = col.Ok() ? col.GetColor() : NULL;
577
578 gtk_text_insert( GTK_TEXT(m_text), fnt, colFg, colBg, txt, txtlen );
579 }
580 else // no style
581 {
582 /* we'll insert at the last position */
583 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
584 gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len );
585 }
586
587 /* bring editable's cursor uptodate. bug in GTK. */
588 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
589 }
590 else // single line
591 {
592 gtk_entry_append_text( GTK_ENTRY(m_text), txt );
593 }
594 }
595
596 wxString wxTextCtrl::GetLineText( long lineNo ) const
597 {
598 if (m_windowStyle & wxTE_MULTILINE)
599 {
600 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
601 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
602
603 if (text)
604 {
605 wxString buf(wxT(""));
606 long i;
607 int currentLine = 0;
608 for (i = 0; currentLine != lineNo && text[i]; i++ )
609 if (text[i] == '\n')
610 currentLine++;
611 // Now get the text
612 int j;
613 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
614 buf += text[i];
615
616 g_free( text );
617 return buf;
618 }
619 else
620 return wxEmptyString;
621 }
622 else
623 {
624 if (lineNo == 0) return GetValue();
625 return wxEmptyString;
626 }
627 }
628
629 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
630 {
631 /* If you implement this, don't forget to update the documentation!
632 * (file docs/latex/wx/text.tex) */
633 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
634 }
635
636 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
637 {
638 if ( m_windowStyle & wxTE_MULTILINE )
639 {
640 wxString text = GetValue();
641
642 // cast to prevent warning. But pos really should've been unsigned.
643 if( (unsigned long)pos > text.Len() )
644 return FALSE;
645
646 *x=0; // First Col
647 *y=0; // First Line
648
649 const wxChar* stop = text.c_str() + pos;
650 for ( const wxChar *p = text.c_str(); p < stop; p++ )
651 {
652 if (*p == wxT('\n'))
653 {
654 (*y)++;
655 *x=0;
656 }
657 else
658 (*x)++;
659 }
660 }
661 else // single line control
662 {
663 if ( pos <= GTK_ENTRY(m_text)->text_length )
664 {
665 *y = 0;
666 *x = pos;
667 }
668 else
669 {
670 // index out of bounds
671 return FALSE;
672 }
673 }
674
675 return TRUE;
676 }
677
678 long wxTextCtrl::XYToPosition(long x, long y ) const
679 {
680 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
681
682 long pos=0;
683 for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
684
685 pos += x;
686 return pos;
687 }
688
689 int wxTextCtrl::GetLineLength(long lineNo) const
690 {
691 wxString str = GetLineText (lineNo);
692 return (int) str.Length();
693 }
694
695 int wxTextCtrl::GetNumberOfLines() const
696 {
697 if (m_windowStyle & wxTE_MULTILINE)
698 {
699 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
700 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
701
702 if (text)
703 {
704 int currentLine = 0;
705 for (int i = 0; i < len; i++ )
706 {
707 if (text[i] == '\n')
708 currentLine++;
709 }
710 g_free( text );
711
712 // currentLine is 0 based, add 1 to get number of lines
713 return currentLine + 1;
714 }
715 else
716 {
717 return 0;
718 }
719 }
720 else
721 {
722 return 1;
723 }
724 }
725
726 void wxTextCtrl::SetInsertionPoint( long pos )
727 {
728 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
729
730 if (m_windowStyle & wxTE_MULTILINE)
731 {
732 /* seems to be broken in GTK 1.0.X:
733 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
734
735 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text),
736 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
737
738 /* we fake a set_point by inserting and deleting. as the user
739 isn't supposed to get to know about thos non-sense, we
740 disconnect so that no events are sent to the user program. */
741
742 gint tmp = (gint)pos;
743 gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp );
744 gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp );
745
746 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
747 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
748
749 /* bring editable's cursor uptodate. another bug in GTK. */
750
751 GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) );
752 }
753 else
754 {
755 gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos );
756
757 /* bring editable's cursor uptodate. bug in GTK. */
758
759 GTK_EDITABLE(m_text)->current_pos = (guint32)pos;
760 }
761 }
762
763 void wxTextCtrl::SetInsertionPointEnd()
764 {
765 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
766
767 if (m_windowStyle & wxTE_MULTILINE)
768 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text)));
769 else
770 gtk_entry_set_position( GTK_ENTRY(m_text), -1 );
771 }
772
773 void wxTextCtrl::SetEditable( bool editable )
774 {
775 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
776
777 if (m_windowStyle & wxTE_MULTILINE)
778 gtk_text_set_editable( GTK_TEXT(m_text), editable );
779 else
780 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
781 }
782
783 bool wxTextCtrl::Enable( bool enable )
784 {
785 if (!wxWindowBase::Enable(enable))
786 {
787 // nothing to do
788 return FALSE;
789 }
790
791 if (m_windowStyle & wxTE_MULTILINE)
792 {
793 gtk_text_set_editable( GTK_TEXT(m_text), enable );
794 OnParentEnable(enable);
795 }
796 else
797 {
798 gtk_widget_set_sensitive( m_text, enable );
799 }
800
801 return TRUE;
802 }
803
804 // wxGTK-specific: called recursively by Enable,
805 // to give widgets an oppprtunity to correct their colours after they
806 // have been changed by Enable
807 void wxTextCtrl::OnParentEnable( bool enable )
808 {
809 // If we have a custom background colour, we use this colour in both
810 // disabled and enabled mode, or we end up with a different colour under the
811 // text.
812 wxColour oldColour = GetBackgroundColour();
813 if (oldColour.Ok())
814 {
815 // Need to set twice or it'll optimize the useful stuff out
816 if (oldColour == * wxWHITE)
817 SetBackgroundColour(*wxBLACK);
818 else
819 SetBackgroundColour(*wxWHITE);
820 SetBackgroundColour(oldColour);
821 }
822 }
823
824 void wxTextCtrl::DiscardEdits()
825 {
826 m_modified = FALSE;
827 }
828
829 void wxTextCtrl::SetSelection( long from, long to )
830 {
831 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
832
833 if ( (m_windowStyle & wxTE_MULTILINE) &&
834 !GTK_TEXT(m_text)->line_start_cache )
835 {
836 // tell the programmer that it didn't work
837 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
838 return;
839 }
840
841 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
842 }
843
844 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
845 {
846 // SetInsertionPoint( pos );
847 }
848
849 long wxTextCtrl::GetInsertionPoint() const
850 {
851 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
852
853 return (long) GTK_EDITABLE(m_text)->current_pos;
854 }
855
856 long wxTextCtrl::GetLastPosition() const
857 {
858 wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") );
859
860 int pos = 0;
861 if (m_windowStyle & wxTE_MULTILINE)
862 pos = gtk_text_get_length( GTK_TEXT(m_text) );
863 else
864 pos = GTK_ENTRY(m_text)->text_length;
865
866 return (long)pos;
867 }
868
869 void wxTextCtrl::Remove( long from, long to )
870 {
871 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
872
873 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
874 }
875
876 void wxTextCtrl::Replace( long from, long to, const wxString &value )
877 {
878 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
879
880 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
881
882 if (!value.IsEmpty())
883 {
884 gint pos = (gint)from;
885 #if wxUSE_UNICODE
886 wxWX2MBbuf buf = value.mbc_str();
887 gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos );
888 #else
889 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
890 #endif
891 }
892 }
893
894 void wxTextCtrl::Cut()
895 {
896 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
897
898 #if (GTK_MINOR_VERSION > 0)
899 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
900 #else
901 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
902 #endif
903 }
904
905 void wxTextCtrl::Copy()
906 {
907 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
908
909 #if (GTK_MINOR_VERSION > 0)
910 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
911 #else
912 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
913 #endif
914 }
915
916 void wxTextCtrl::Paste()
917 {
918 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
919
920 #if (GTK_MINOR_VERSION > 0)
921 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
922 #else
923 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
924 #endif
925 }
926
927 // Undo/redo
928 void wxTextCtrl::Undo()
929 {
930 // TODO
931 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
932 }
933
934 void wxTextCtrl::Redo()
935 {
936 // TODO
937 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
938 }
939
940 bool wxTextCtrl::CanUndo() const
941 {
942 // TODO
943 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
944 return FALSE;
945 }
946
947 bool wxTextCtrl::CanRedo() const
948 {
949 // TODO
950 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
951 return FALSE;
952 }
953
954 // If the return values from and to are the same, there is no
955 // selection.
956 void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const
957 {
958 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
959
960 long from, to;
961 if ( !(GTK_EDITABLE(m_text)->has_selection) )
962 {
963 from =
964 to = GetInsertionPoint();
965 }
966 else // got selection
967 {
968 from = (long) GTK_EDITABLE(m_text)->selection_start_pos;
969 to = (long) GTK_EDITABLE(m_text)->selection_end_pos;
970
971 if ( from > to )
972 {
973 // exchange them to be compatible with wxMSW
974 long tmp = from;
975 from = to;
976 to = tmp;
977 }
978 }
979
980 if ( fromOut )
981 *fromOut = from;
982 if ( toOut )
983 *toOut = to;
984 }
985
986 bool wxTextCtrl::IsEditable() const
987 {
988 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
989
990 return GTK_EDITABLE(m_text)->editable;
991 }
992
993 bool wxTextCtrl::IsModified() const
994 {
995 return m_modified;
996 }
997
998 void wxTextCtrl::Clear()
999 {
1000 SetValue( wxT("") );
1001 }
1002
1003 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
1004 {
1005 wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
1006
1007 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
1008 {
1009 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1010 event.SetEventObject(this);
1011 event.SetString(GetValue());
1012 if (GetEventHandler()->ProcessEvent(event)) return;
1013 }
1014
1015 if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE))
1016 {
1017 wxWindow *top_frame = m_parent;
1018 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
1019 top_frame = top_frame->GetParent();
1020 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
1021
1022 if (window->default_widget)
1023 {
1024 gtk_widget_activate (window->default_widget);
1025 return;
1026 }
1027 }
1028
1029 key_event.Skip();
1030 }
1031
1032 GtkWidget* wxTextCtrl::GetConnectWidget()
1033 {
1034 return GTK_WIDGET(m_text);
1035 }
1036
1037 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
1038 {
1039 if (m_windowStyle & wxTE_MULTILINE)
1040 return (window == GTK_TEXT(m_text)->text_area);
1041 else
1042 return (window == GTK_ENTRY(m_text)->text_area);
1043 }
1044
1045 // the font will change for subsequent text insertiongs
1046 bool wxTextCtrl::SetFont( const wxFont &font )
1047 {
1048 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1049
1050 if ( !wxWindowBase::SetFont(font) )
1051 {
1052 // font didn't change, nothing to do
1053 return FALSE;
1054 }
1055
1056 if ( m_windowStyle & wxTE_MULTILINE )
1057 {
1058 m_updateFont = TRUE;
1059
1060 m_defaultStyle.SetFont(font);
1061
1062 ChangeFontGlobally();
1063 }
1064
1065 return TRUE;
1066 }
1067
1068 void wxTextCtrl::ChangeFontGlobally()
1069 {
1070 // this method is very inefficient and hence should be called as rarely as
1071 // possible!
1072 wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont,
1073 _T("shouldn't be called for single line controls") );
1074
1075 wxString value = GetValue();
1076 if ( !value.IsEmpty() )
1077 {
1078 m_updateFont = FALSE;
1079
1080 Clear();
1081 AppendText(value);
1082 }
1083 }
1084
1085 void wxTextCtrl::UpdateFontIfNeeded()
1086 {
1087 if ( m_updateFont )
1088 ChangeFontGlobally();
1089 }
1090
1091 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1092 {
1093 if ( !wxControl::SetForegroundColour(colour) )
1094 return FALSE;
1095
1096 // update default fg colour too
1097 m_defaultStyle.SetTextColour(colour);
1098
1099 return TRUE;
1100 }
1101
1102 bool wxTextCtrl::SetBackgroundColour( const wxColour &colour )
1103 {
1104 wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") );
1105
1106 if ( !wxControl::SetBackgroundColour( colour ) )
1107 return FALSE;
1108
1109 if (!m_widget->window)
1110 return FALSE;
1111
1112 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
1113 if (sysbg.Red() == colour.Red() &&
1114 sysbg.Green() == colour.Green() &&
1115 sysbg.Blue() == colour.Blue())
1116 {
1117 return FALSE; // FIXME or TRUE?
1118 }
1119
1120 if (!m_backgroundColour.Ok())
1121 return FALSE;
1122
1123 if (m_windowStyle & wxTE_MULTILINE)
1124 {
1125 GdkWindow *window = GTK_TEXT(m_text)->text_area;
1126 if (!window)
1127 return FALSE;
1128 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1129 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1130 gdk_window_clear( window );
1131 }
1132
1133 // change active background color too
1134 m_defaultStyle.SetBackgroundColour( colour );
1135
1136 return TRUE;
1137 }
1138
1139 bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr &style )
1140 {
1141 /* VERY dirty way to do that - removes the required text and re-adds it
1142 with styling (FIXME) */
1143 if ( m_windowStyle & wxTE_MULTILINE )
1144 {
1145 if ( style.IsDefault() )
1146 {
1147 // nothing to do
1148 return TRUE;
1149 }
1150
1151 gint l = gtk_text_get_length( GTK_TEXT(m_text) );
1152
1153 wxCHECK_MSG( start >= 0 && end <= l, FALSE,
1154 _T("invalid range in wxTextCtrl::SetStyle") );
1155
1156 gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) );
1157 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end );
1158 wxString tmp(text,*wxConvCurrent);
1159 g_free( text );
1160
1161 gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end );
1162 gtk_editable_set_position( GTK_EDITABLE(m_text), start );
1163
1164 #if wxUSE_UNICODE
1165 wxWX2MBbuf buf = tmp.mbc_str();
1166 const char *txt = buf;
1167 size_t txtlen = strlen(buf);
1168 #else
1169 const char *txt = tmp;
1170 size_t txtlen = tmp.length();
1171 #endif
1172
1173 GdkFont *font = style.HasFont()
1174 ? style.GetFont().GetInternalFont()
1175 : NULL;
1176
1177 GdkColor *colFg = style.HasTextColour()
1178 ? style.GetTextColour().GetColor()
1179 : NULL;
1180
1181 GdkColor *colBg = style.HasBackgroundColour()
1182 ? style.GetBackgroundColour().GetColor()
1183 : NULL;
1184
1185 gtk_text_insert( GTK_TEXT(m_text), font, colFg, colBg, txt, txtlen );
1186
1187 /* does not seem to help under GTK+ 1.2 !!!
1188 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1189 SetInsertionPoint( old_pos );
1190 return TRUE;
1191 }
1192 else // singe line
1193 {
1194 // cannot do this for GTK+'s Entry widget
1195 return FALSE;
1196 }
1197 }
1198
1199 void wxTextCtrl::ApplyWidgetStyle()
1200 {
1201 if (m_windowStyle & wxTE_MULTILINE)
1202 {
1203 // how ?
1204 }
1205 else
1206 {
1207 SetWidgetStyle();
1208 gtk_widget_set_style( m_text, m_widgetStyle );
1209 }
1210 }
1211
1212 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1213 {
1214 Cut();
1215 }
1216
1217 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1218 {
1219 Copy();
1220 }
1221
1222 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1223 {
1224 Paste();
1225 }
1226
1227 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1228 {
1229 Undo();
1230 }
1231
1232 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1233 {
1234 Redo();
1235 }
1236
1237 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1238 {
1239 event.Enable( CanCut() );
1240 }
1241
1242 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1243 {
1244 event.Enable( CanCopy() );
1245 }
1246
1247 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1248 {
1249 event.Enable( CanPaste() );
1250 }
1251
1252 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1253 {
1254 event.Enable( CanUndo() );
1255 }
1256
1257 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1258 {
1259 event.Enable( CanRedo() );
1260 }
1261
1262 void wxTextCtrl::OnInternalIdle()
1263 {
1264 wxCursor cursor = m_cursor;
1265 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1266
1267 if (cursor.Ok())
1268 {
1269 GdkWindow *window = (GdkWindow*) NULL;
1270 if (HasFlag(wxTE_MULTILINE))
1271 window = GTK_TEXT(m_text)->text_area;
1272 else
1273 window = GTK_ENTRY(m_text)->text_area;
1274
1275 if (window)
1276 gdk_window_set_cursor( window, cursor.GetCursor() );
1277
1278 if (!g_globalCursor.Ok())
1279 cursor = *wxSTANDARD_CURSOR;
1280
1281 window = m_widget->window;
1282 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
1283 gdk_window_set_cursor( window, cursor.GetCursor() );
1284 }
1285
1286 UpdateWindowUI();
1287 }
1288
1289 wxSize wxTextCtrl::DoGetBestSize() const
1290 {
1291 // FIXME should be different for multi-line controls...
1292 wxSize ret( wxControl::DoGetBestSize() );
1293 return wxSize(80, ret.y);
1294 }
1295
1296 // ----------------------------------------------------------------------------
1297 // freeze/thaw
1298 // ----------------------------------------------------------------------------
1299
1300 void wxTextCtrl::Freeze()
1301 {
1302 if ( HasFlag(wxTE_MULTILINE) )
1303 {
1304 gtk_text_freeze(GTK_TEXT(m_text));
1305 }
1306 }
1307
1308 void wxTextCtrl::Thaw()
1309 {
1310 if ( HasFlag(wxTE_MULTILINE) )
1311 {
1312 GTK_TEXT(m_text)->vadj->value = 0.0;
1313
1314 gtk_text_thaw(GTK_TEXT(m_text));
1315 }
1316 }
1317
1318 // ----------------------------------------------------------------------------
1319 // scrolling
1320 // ----------------------------------------------------------------------------
1321
1322 GtkAdjustment *wxTextCtrl::GetVAdj() const
1323 {
1324 return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL;
1325 }
1326
1327 bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff)
1328 {
1329 float value = adj->value + diff;
1330
1331 if ( value < 0 )
1332 value = 0;
1333
1334 float upper = adj->upper - adj->page_size;
1335 if ( value > upper )
1336 value = upper;
1337
1338 // did we noticeably change the scroll position?
1339 if ( fabs(adj->value - value) < 0.2 )
1340 {
1341 // well, this is what Robert does in wxScrollBar, so it must be good...
1342 return FALSE;
1343 }
1344
1345 adj->value = value;
1346
1347 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
1348
1349 return TRUE;
1350 }
1351
1352 bool wxTextCtrl::ScrollLines(int lines)
1353 {
1354 GtkAdjustment *adj = GetVAdj();
1355 if ( !adj )
1356 return FALSE;
1357
1358 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1359 static const int KEY_SCROLL_PIXELS = 10;
1360
1361 return DoScroll(adj, lines*KEY_SCROLL_PIXELS);
1362 }
1363
1364 bool wxTextCtrl::ScrollPages(int pages)
1365 {
1366 GtkAdjustment *adj = GetVAdj();
1367 if ( !adj )
1368 return FALSE;
1369
1370 return DoScroll(adj, (int)ceil(pages*adj->page_increment));
1371 }
1372