]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
all delete functions now send delete notification event
[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/settings.h"
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <ctype.h>
22
23 #include "gdk/gdkkeysyms.h"
24
25 //-----------------------------------------------------------------------------
26 // data
27 //-----------------------------------------------------------------------------
28
29 extern bool g_blockEventsOnDrag;
30
31 //-----------------------------------------------------------------------------
32 // "changed"
33 //-----------------------------------------------------------------------------
34
35 static void
36 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
37 {
38 win->SetModified();
39
40 win->CalculateScrollbar();
41
42 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId );
43 wxString val( win->GetValue() );
44 if (!val.IsNull()) event.m_commandString = WXSTRINGCAST val;
45 event.SetEventObject( win );
46 win->GetEventHandler()->ProcessEvent( event );
47 }
48
49 //-----------------------------------------------------------------------------
50 // "size_allocate"
51 //-----------------------------------------------------------------------------
52
53 static void
54 gtk_text_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* WXUNUSED(alloc), wxTextCtrl *win )
55 {
56 win->CalculateScrollbar();
57 }
58
59 //-----------------------------------------------------------------------------
60 // wxTextCtrl
61 //-----------------------------------------------------------------------------
62
63 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
64
65 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
66 EVT_CHAR(wxTextCtrl::OnChar)
67 END_EVENT_TABLE()
68
69 wxTextCtrl::wxTextCtrl() : streambuf()
70 {
71 if (allocate()) setp(base(),ebuf());
72
73 m_modified = FALSE;
74 }
75
76 wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
77 const wxPoint &pos, const wxSize &size,
78 int style, const wxValidator& validator, const wxString &name ) : streambuf()
79 {
80 if (allocate()) setp(base(),ebuf());
81
82 m_modified = FALSE;
83 Create( parent, id, value, pos, size, style, validator, name );
84 }
85
86 bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
87 const wxPoint &pos, const wxSize &size,
88 int style, const wxValidator& validator, const wxString &name )
89 {
90 m_needParent = TRUE;
91 m_acceptsFocus = TRUE;
92
93 PreCreation( parent, id, pos, size, style, name );
94
95 SetValidator( validator );
96
97 m_vScrollbarVisible = TRUE;
98
99 bool multi_line = (style & wxTE_MULTILINE) != 0;
100 if ( multi_line )
101 {
102 // a multi-line edit control: create a vertical scrollbar by default and
103 // horizontal if requested
104 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
105
106 // create our control...
107 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
108
109 // ... and put into the upper left hand corner of the table
110 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
111 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
112 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
113 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
114 0, 0);
115
116 // put the horizontal scrollbar in the lower left hand corner
117 if (bHasHScrollbar)
118 {
119 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
120 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
121 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
122 GTK_FILL,
123 0, 0);
124 gtk_widget_show(hscrollbar);
125 }
126
127 // finally, put the vertical scrollbar in the upper right corner
128 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
129 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
130 GTK_FILL,
131 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
132 0, 0);
133 gtk_widget_show( m_vScrollbar );
134
135 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
136 GTK_SIGNAL_FUNC(gtk_text_size_callback), (gpointer)this );
137 }
138 else
139 {
140 // a single-line text control: no need for scrollbars
141 m_widget =
142 m_text = gtk_entry_new();
143 }
144
145 wxSize newSize = size;
146 if (newSize.x == -1) newSize.x = 80;
147 if (newSize.y == -1) newSize.y = 26;
148 SetSize( newSize.x, newSize.y );
149
150 m_parent->AddChild( this );
151
152 (m_parent->m_insertCallback)( m_parent, this );
153
154 PostCreation();
155
156 if (multi_line)
157 {
158 gtk_widget_realize(m_text);
159 gtk_widget_show(m_text);
160 }
161
162 // we want to be notified about text changes
163 gtk_signal_connect( GTK_OBJECT(m_text), "changed",
164 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
165
166 if (!value.IsNull())
167 {
168 gint tmp = 0;
169 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
170 SetInsertionPointEnd();
171 }
172
173 if (style & wxTE_PASSWORD)
174 {
175 if (!multi_line)
176 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
177 }
178
179 if (style & wxTE_READONLY)
180 {
181 if (!multi_line)
182 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
183 }
184 else
185 {
186 if (multi_line)
187 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
188 }
189
190 Show( TRUE );
191
192 SetBackgroundColour( parent->GetBackgroundColour() );
193 SetForegroundColour( parent->GetForegroundColour() );
194
195 return TRUE;
196 }
197
198 void wxTextCtrl::CalculateScrollbar()
199 {
200 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
201
202 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
203
204 if (adj->upper - adj->page_size < 0.8)
205 {
206 if (m_vScrollbarVisible)
207 {
208 gtk_widget_hide( m_vScrollbar );
209
210 m_vScrollbarVisible = FALSE;
211 }
212 }
213 else
214 {
215 if (!m_vScrollbarVisible)
216 {
217 gtk_widget_show( m_vScrollbar );
218
219 m_vScrollbarVisible = TRUE;
220 }
221 }
222 }
223
224 wxString wxTextCtrl::GetValue() const
225 {
226 wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" );
227
228 wxString tmp;
229 if (m_windowStyle & wxTE_MULTILINE)
230 {
231 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
232 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
233 tmp = text;
234 g_free( text );
235 }
236 else
237 {
238 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
239 }
240 return tmp;
241 }
242
243 void wxTextCtrl::SetValue( const wxString &value )
244 {
245 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
246
247 wxString tmp = "";
248 if (!value.IsNull()) tmp = value;
249 if (m_windowStyle & wxTE_MULTILINE)
250 {
251 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
252 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
253 len = 0;
254 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
255 }
256 else
257 {
258 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
259 }
260 }
261
262 void wxTextCtrl::WriteText( const wxString &text )
263 {
264 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
265
266 if (text.IsNull()) return;
267
268 if (m_windowStyle & wxTE_MULTILINE)
269 {
270 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
271 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
272 }
273 else
274 {
275 gtk_entry_append_text( GTK_ENTRY(m_text), text );
276 }
277 }
278
279 bool wxTextCtrl::LoadFile( const wxString &file )
280 {
281 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
282
283 if (!wxFileExists(file)) return FALSE;
284
285 Clear();
286
287 FILE *fp = NULL;
288 struct stat statb;
289
290 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
291 !(fp = fopen ((char*) (const char*) file, "r")))
292 {
293 return FALSE;
294 }
295 else
296 {
297 gint len = statb.st_size;
298 char *text;
299 if (!(text = (char*)malloc ((unsigned) (len + 1))))
300 {
301 fclose (fp);
302 return FALSE;
303 }
304 if (fread (text, sizeof (char), len, fp) != (size_t) len)
305 {
306 }
307 fclose (fp);
308
309 text[len] = 0;
310
311 if (m_windowStyle & wxTE_MULTILINE)
312 {
313 gint pos = 0;
314 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos );
315 }
316 else
317 {
318 gtk_entry_set_text( GTK_ENTRY(m_text), text );
319 }
320
321 free (text);
322 m_modified = FALSE;
323 return TRUE;
324 }
325 return FALSE;
326 }
327
328 bool wxTextCtrl::SaveFile( const wxString &file )
329 {
330 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
331
332 if (file == "") return FALSE;
333
334 FILE *fp;
335
336 if (!(fp = fopen ((char*) (const char*) file, "w")))
337 {
338 return FALSE;
339 }
340 else
341 {
342 char *text = NULL;
343 gint len = 0;
344
345 if (m_windowStyle & wxTE_MULTILINE)
346 {
347 len = gtk_text_get_length( GTK_TEXT(m_text) );
348 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
349 }
350 else
351 {
352 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
353 }
354
355 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
356 {
357 // Did not write whole file
358 }
359
360 // Make sure newline terminates the file
361 if (text[len - 1] != '\n')
362 fputc ('\n', fp);
363
364 fclose (fp);
365
366 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
367
368 m_modified = FALSE;
369 return TRUE;
370 }
371
372 return TRUE;
373 }
374
375 wxString wxTextCtrl::GetLineText( long lineNo ) const
376 {
377 if (m_windowStyle & wxTE_MULTILINE)
378 {
379 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
380 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
381
382 if (text)
383 {
384 wxString buf("");
385 long i;
386 int currentLine = 0;
387 for (i = 0; currentLine != lineNo && text[i]; i++ )
388 if (text[i] == '\n')
389 currentLine++;
390 // Now get the text
391 int j;
392 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
393 buf += text[i];
394
395 g_free( text );
396 return buf;
397 }
398 else
399 return wxEmptyString;
400 }
401 else
402 {
403 if (lineNo == 0) return GetValue();
404 return wxEmptyString;
405 }
406 }
407
408 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
409 {
410 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
411 }
412
413 long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
414 {
415 if (!(m_windowStyle & wxTE_MULTILINE))
416 return 0;
417 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
418 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
419 if(!text)
420 return 0;
421 if( pos >= len)
422 return pos=len-1;
423
424 *x=1; // Col 1
425 *y=1; // Line 1
426 for (int i = 0; i < pos; i++ )
427 {
428 if (text[i] == '\n')
429 {
430 (*y)++;
431 *x=1;
432 }
433 else
434 (*x)++;
435 }
436 g_free( text );
437 return 1;
438 }
439
440 long wxTextCtrl::XYToPosition(long x, long y ) const
441 {
442 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
443
444 long pos=0;
445
446 for( int i=1; i<y; i++ ) pos += GetLineLength(i);
447
448 pos +=x-1; // Pos start with 0
449 return pos;
450 }
451
452 int wxTextCtrl::GetLineLength(long lineNo) const
453 {
454 wxString str = GetLineText (lineNo);
455 return (int) str.Length();
456 }
457
458 int wxTextCtrl::GetNumberOfLines() const
459 {
460 if (m_windowStyle & wxTE_MULTILINE)
461 {
462 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
463 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
464
465 if (text)
466 {
467 int currentLine = 0;
468 for (int i = 0; i < len; i++ )
469 {
470 if (text[i] == '\n')
471 currentLine++;
472 }
473 g_free( text );
474 return currentLine;
475 }
476 else
477 {
478 return 0;
479 }
480 }
481 else
482 {
483 return 1;
484 }
485 }
486
487 void wxTextCtrl::SetInsertionPoint( long pos )
488 {
489 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
490
491 int tmp = (int) pos;
492 if (m_windowStyle & wxTE_MULTILINE)
493 gtk_text_set_point( GTK_TEXT(m_text), tmp );
494 else
495 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
496 }
497
498 void wxTextCtrl::SetInsertionPointEnd()
499 {
500 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
501
502 int pos = 0;
503 if (m_windowStyle & wxTE_MULTILINE)
504 pos = gtk_text_get_length( GTK_TEXT(m_text) );
505 else
506 pos = GTK_ENTRY(m_text)->text_length;
507
508 SetInsertionPoint((pos-1)>0 ? (pos-1):0);
509 }
510
511 void wxTextCtrl::SetEditable( bool editable )
512 {
513 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
514
515 if (m_windowStyle & wxTE_MULTILINE)
516 gtk_text_set_editable( GTK_TEXT(m_text), editable );
517 else
518 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
519 }
520
521 void wxTextCtrl::SetSelection( long from, long to )
522 {
523 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
524
525 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
526 }
527
528 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
529 {
530 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
531 }
532
533 long wxTextCtrl::GetInsertionPoint() const
534 {
535 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
536
537 return (long) GTK_EDITABLE(m_text)->current_pos;
538 }
539
540 long wxTextCtrl::GetLastPosition() const
541 {
542 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
543
544 int pos = 0;
545 if (m_windowStyle & wxTE_MULTILINE)
546 pos = gtk_text_get_length( GTK_TEXT(m_text) );
547 else
548 pos = GTK_ENTRY(m_text)->text_length;
549
550 return (long)pos-1;
551 }
552
553 void wxTextCtrl::Remove( long from, long to )
554 {
555 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
556
557 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
558 }
559
560 void wxTextCtrl::Replace( long from, long to, const wxString &value )
561 {
562 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
563
564 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
565 if (value.IsNull()) return;
566 gint pos = (gint)from;
567 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
568 }
569
570 void wxTextCtrl::Cut()
571 {
572 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
573
574 #if (GTK_MINOR_VERSION == 1)
575 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
576 #else
577 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
578 #endif
579 }
580
581 void wxTextCtrl::Copy()
582 {
583 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
584
585 #if (GTK_MINOR_VERSION == 1)
586 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
587 #else
588 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
589 #endif
590 }
591
592 void wxTextCtrl::Paste()
593 {
594 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
595
596 #if (GTK_MINOR_VERSION == 1)
597 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
598 #else
599 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
600 #endif
601 }
602
603 void wxTextCtrl::Clear()
604 {
605 SetValue( "" );
606 }
607
608 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
609 {
610 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
611
612 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
613 {
614 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
615 event.SetEventObject(this);
616 if (GetEventHandler()->ProcessEvent(event)) return;
617 }
618
619 key_event.Skip();
620 }
621
622 int wxTextCtrl::overflow( int WXUNUSED(c) )
623 {
624 int len = pptr() - pbase();
625 char *txt = new char[len+1];
626 strncpy(txt, pbase(), len);
627 txt[len] = '\0';
628 (*this) << txt;
629 setp(pbase(), epptr());
630 delete[] txt;
631 return EOF;
632 }
633
634 int wxTextCtrl::sync()
635 {
636 int len = pptr() - pbase();
637 char *txt = new char[len+1];
638 strncpy(txt, pbase(), len);
639 txt[len] = '\0';
640 (*this) << txt;
641 setp(pbase(), epptr());
642 delete[] txt;
643 return 0;
644 }
645
646 int wxTextCtrl::underflow()
647 {
648 return EOF;
649 }
650
651 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
652 {
653 WriteText(s);
654 return *this;
655 }
656
657 wxTextCtrl& wxTextCtrl::operator<<(float f)
658 {
659 static char buf[100];
660 sprintf(buf, "%.2f", f);
661 WriteText(buf);
662 return *this;
663 }
664
665 wxTextCtrl& wxTextCtrl::operator<<(double d)
666 {
667 static char buf[100];
668 sprintf(buf, "%.2f", d);
669 WriteText(buf);
670 return *this;
671 }
672
673 wxTextCtrl& wxTextCtrl::operator<<(int i)
674 {
675 static char buf[100];
676 sprintf(buf, "%i", i);
677 WriteText(buf);
678 return *this;
679 }
680
681 wxTextCtrl& wxTextCtrl::operator<<(long i)
682 {
683 static char buf[100];
684 sprintf(buf, "%ld", i);
685 WriteText(buf);
686 return *this;
687 }
688
689 wxTextCtrl& wxTextCtrl::operator<<(const char c)
690 {
691 char buf[2];
692
693 buf[0] = c;
694 buf[1] = 0;
695 WriteText(buf);
696 return *this;
697 }
698
699 GtkWidget* wxTextCtrl::GetConnectWidget()
700 {
701 return GTK_WIDGET(m_text);
702 }
703
704 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
705 {
706 if (m_windowStyle & wxTE_MULTILINE)
707 return (window == GTK_TEXT(m_text)->text_area);
708 else
709 return (window == GTK_ENTRY(m_text)->text_area);
710 }
711
712 void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
713 {
714 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
715
716 // doesn't work
717 }
718
719 void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
720 {
721 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
722
723 // doesn't work
724 }
725
726 void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
727 {
728 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
729
730 wxControl::SetBackgroundColour( colour );
731
732 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
733 if (sysbg.Red() == colour.Red() &&
734 sysbg.Green() == colour.Green() &&
735 sysbg.Blue() == colour.Blue())
736 {
737 return;
738 }
739
740 if (!m_backgroundColour.Ok()) return;
741
742 if (m_windowStyle & wxTE_MULTILINE)
743 {
744 GdkWindow *window = GTK_TEXT(m_text)->text_area;
745 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
746 gdk_window_set_background( window, m_backgroundColour.GetColor() );
747 gdk_window_clear( window );
748 }
749 }
750
751 void wxTextCtrl::ApplyWidgetStyle()
752 {
753 if (m_windowStyle & wxTE_MULTILINE)
754 {
755 // how ?
756 }
757 else
758 {
759 SetWidgetStyle();
760 gtk_widget_set_style( m_text, m_widgetStyle );
761 }
762 }
763