]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
wxIsNumeric for values < 0
[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 //-----------------------------------------------------------------------------
24 // "changed"
25 //-----------------------------------------------------------------------------
26
27 static void
28 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
29 {
30 win->SetModified();
31
32 win->CalculateScrollbar();
33
34 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId );
35 wxString val( win->GetValue() );
36 if (!val.IsNull()) event.m_commandString = WXSTRINGCAST val;
37 event.SetEventObject( win );
38 win->GetEventHandler()->ProcessEvent( event );
39 }
40
41 //-----------------------------------------------------------------------------
42 // "size_allocate"
43 //-----------------------------------------------------------------------------
44
45 static void
46 gtk_text_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* WXUNUSED(alloc), wxTextCtrl *win )
47 {
48 win->CalculateScrollbar();
49 }
50
51
52 //-----------------------------------------------------------------------------
53 // wxTextCtrl
54 //-----------------------------------------------------------------------------
55
56 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
57
58 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
59 EVT_CHAR(wxTextCtrl::OnChar)
60 END_EVENT_TABLE()
61
62 wxTextCtrl::wxTextCtrl() : streambuf()
63 {
64 if (allocate()) setp(base(),ebuf());
65
66 m_modified = FALSE;
67 }
68
69 wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
70 const wxPoint &pos, const wxSize &size,
71 int style, const wxValidator& validator, const wxString &name ) : streambuf()
72 {
73 if (allocate()) setp(base(),ebuf());
74
75 m_modified = FALSE;
76 Create( parent, id, value, pos, size, style, validator, name );
77 }
78
79 bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
80 const wxPoint &pos, const wxSize &size,
81 int style, const wxValidator& validator, const wxString &name )
82 {
83 m_needParent = TRUE;
84
85 PreCreation( parent, id, pos, size, style, name );
86
87 SetValidator( validator );
88
89 m_vScrollbarVisible = TRUE;
90
91 bool multi_line = (style & wxTE_MULTILINE) != 0;
92 if ( multi_line )
93 {
94 // a multi-line edit control: create a vertical scrollbar by default and
95 // horizontal if requested
96 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
97
98 // create our control...
99 m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
100
101 // ... and put into the upper left hand corner of the table
102 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
103 gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
104 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
105 (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK),
106 0, 0);
107
108 // put the horizontal scrollbar in the lower left hand corner
109 if (bHasHScrollbar)
110 {
111 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
112 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
113 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL),
114 GTK_FILL,
115 0, 0);
116 gtk_widget_show(hscrollbar);
117 }
118
119 // finally, put the vertical scrollbar in the upper right corner
120 m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj );
121 gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1,
122 GTK_FILL,
123 (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK),
124 0, 0);
125 gtk_widget_show( m_vScrollbar );
126
127 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
128 GTK_SIGNAL_FUNC(gtk_text_size_callback), (gpointer)this );
129 }
130 else
131 {
132 // a single-line text control: no need for scrollbars
133 m_widget =
134 m_text = gtk_entry_new();
135 }
136
137 wxSize newSize = size;
138 if (newSize.x == -1) newSize.x = 80;
139 if (newSize.y == -1) newSize.y = 26;
140 SetSize( newSize.x, newSize.y );
141
142 m_parent->AddChild( this );
143
144 (m_parent->m_insertCallback)( m_parent, this );
145
146 PostCreation();
147
148 if (multi_line)
149 {
150 gtk_widget_realize(m_text);
151 gtk_widget_show(m_text);
152 }
153
154 // we want to be notified about text changes
155 gtk_signal_connect(GTK_OBJECT(m_text), "changed",
156 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
157 (gpointer)this);
158
159 if (!value.IsNull())
160 {
161 gint tmp = 0;
162 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
163 SetInsertionPointEnd();
164 }
165
166 if (style & wxTE_PASSWORD)
167 {
168 if (!multi_line)
169 gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE );
170 }
171
172 if (style & wxTE_READONLY)
173 {
174 if (!multi_line)
175 gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE );
176 }
177 else
178 {
179 if (multi_line)
180 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
181 }
182
183 Show( TRUE );
184
185 SetBackgroundColour( parent->GetBackgroundColour() );
186 SetForegroundColour( parent->GetForegroundColour() );
187
188 return TRUE;
189 }
190
191 void wxTextCtrl::CalculateScrollbar()
192 {
193 if ((m_windowStyle & wxTE_MULTILINE) == 0) return;
194
195 GtkAdjustment *adj = GTK_TEXT(m_text)->vadj;
196
197 if (adj->upper - adj->page_size < 0.8)
198 {
199 if (m_vScrollbarVisible)
200 {
201 gtk_widget_hide( m_vScrollbar );
202
203 m_vScrollbarVisible = FALSE;
204 }
205 }
206 else
207 {
208 if (!m_vScrollbarVisible)
209 {
210 gtk_widget_show( m_vScrollbar );
211
212 m_vScrollbarVisible = TRUE;
213 }
214 }
215 }
216
217 wxString wxTextCtrl::GetValue() const
218 {
219 wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" );
220
221 wxString tmp;
222 if (m_windowStyle & wxTE_MULTILINE)
223 {
224 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
225 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
226 tmp = text;
227 g_free( text );
228 }
229 else
230 {
231 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
232 }
233 return tmp;
234 }
235
236 void wxTextCtrl::SetValue( const wxString &value )
237 {
238 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
239
240 wxString tmp = "";
241 if (!value.IsNull()) tmp = value;
242 if (m_windowStyle & wxTE_MULTILINE)
243 {
244 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
245 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
246 len = 0;
247 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
248 }
249 else
250 {
251 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
252 }
253 }
254
255 void wxTextCtrl::WriteText( const wxString &text )
256 {
257 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
258
259 if (text.IsNull()) return;
260
261 if (m_windowStyle & wxTE_MULTILINE)
262 {
263 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
264 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
265 }
266 else
267 {
268 gtk_entry_append_text( GTK_ENTRY(m_text), text );
269 }
270 }
271
272 bool wxTextCtrl::LoadFile( const wxString &file )
273 {
274 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
275
276 if (!wxFileExists(file)) return FALSE;
277
278 Clear();
279
280 FILE *fp = NULL;
281 struct stat statb;
282
283 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
284 !(fp = fopen ((char*) (const char*) file, "r")))
285 {
286 return FALSE;
287 }
288 else
289 {
290 gint len = statb.st_size;
291 char *text;
292 if (!(text = (char*)malloc ((unsigned) (len + 1))))
293 {
294 fclose (fp);
295 return FALSE;
296 }
297 if (fread (text, sizeof (char), len, fp) != (size_t) len)
298 {
299 }
300 fclose (fp);
301
302 text[len] = 0;
303
304 if (m_windowStyle & wxTE_MULTILINE)
305 {
306 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, 0, &len );
307 }
308 else
309 {
310 gtk_entry_set_text( GTK_ENTRY(m_text), text );
311 }
312
313 free (text);
314 m_modified = FALSE;
315 return TRUE;
316 }
317 return FALSE;
318 }
319
320 bool wxTextCtrl::SaveFile( const wxString &file )
321 {
322 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
323
324 if (file == "") return FALSE;
325
326 FILE *fp;
327
328 if (!(fp = fopen ((char*) (const char*) file, "w")))
329 {
330 return FALSE;
331 }
332 else
333 {
334 char *text = NULL;
335 gint len = 0;
336
337 if (m_windowStyle & wxTE_MULTILINE)
338 {
339 len = gtk_text_get_length( GTK_TEXT(m_text) );
340 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
341 }
342 else
343 {
344 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
345 }
346
347 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
348 {
349 // Did not write whole file
350 }
351
352 // Make sure newline terminates the file
353 if (text[len - 1] != '\n')
354 fputc ('\n', fp);
355
356 fclose (fp);
357
358 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
359
360 m_modified = FALSE;
361 return TRUE;
362 }
363
364 return TRUE;
365 }
366
367 wxString wxTextCtrl::GetLineText( long lineNo ) const
368 {
369 if (m_windowStyle & wxTE_MULTILINE)
370 {
371 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
372 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
373
374 if (text)
375 {
376 wxString buf("");
377 long i;
378 int currentLine = 0;
379 for (i = 0; currentLine != lineNo && text[i]; i++ )
380 if (text[i] == '\n')
381 currentLine++;
382 // Now get the text
383 int j;
384 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
385 buf += text[i];
386
387 g_free( text );
388 return buf;
389 }
390 else
391 return wxEmptyString;
392 }
393 else
394 {
395 if (lineNo == 0) return GetValue();
396 return wxEmptyString;
397 }
398 }
399
400 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
401 {
402 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
403 }
404
405 long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
406 {
407 if (!(m_windowStyle & wxTE_MULTILINE))
408 return 0;
409 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
410 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
411 if(!text)
412 return 0;
413 if( pos >= len)
414 return pos=len-1;
415
416 *x=1; // Col 1
417 *y=1; // Line 1
418 for (int i = 0; i < pos; i++ )
419 {
420 if (text[i] == '\n')
421 {
422 (*y)++;
423 *x=1;
424 }
425 else
426 (*x)++;
427 }
428 g_free( text );
429 return 1;
430 }
431
432 long wxTextCtrl::XYToPosition(long x, long y ) const
433 {
434 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
435
436 long pos=0;
437
438 for( int i=1; i<y; i++ ) pos += GetLineLength(i);
439
440 pos +=x-1; // Pos start with 0
441 return pos;
442 }
443
444 int wxTextCtrl::GetLineLength(long lineNo) const
445 {
446 wxString str = GetLineText (lineNo);
447 return (int) str.Length();
448 }
449
450 int wxTextCtrl::GetNumberOfLines() const
451 {
452 if (m_windowStyle & wxTE_MULTILINE)
453 {
454 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
455 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
456
457 if (text)
458 {
459 int currentLine = 0;
460 for (int i = 0; i < len; i++ )
461 {
462 if (text[i] == '\n')
463 currentLine++;
464 }
465 g_free( text );
466 return currentLine;
467 }
468 else
469 {
470 return 0;
471 }
472 }
473 else
474 {
475 return 1;
476 }
477 }
478
479 void wxTextCtrl::SetInsertionPoint( long pos )
480 {
481 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
482
483 int tmp = (int) pos;
484 if (m_windowStyle & wxTE_MULTILINE)
485 gtk_text_set_point( GTK_TEXT(m_text), tmp );
486 else
487 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
488 }
489
490 void wxTextCtrl::SetInsertionPointEnd()
491 {
492 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
493
494 int pos = 0;
495 if (m_windowStyle & wxTE_MULTILINE)
496 pos = gtk_text_get_length( GTK_TEXT(m_text) );
497 else
498 pos = GTK_ENTRY(m_text)->text_length;
499
500 SetInsertionPoint((pos-1)>0 ? (pos-1):0);
501 }
502
503 void wxTextCtrl::SetEditable( bool editable )
504 {
505 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
506
507 if (m_windowStyle & wxTE_MULTILINE)
508 gtk_text_set_editable( GTK_TEXT(m_text), editable );
509 else
510 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
511 }
512
513 void wxTextCtrl::SetSelection( long from, long to )
514 {
515 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
516
517 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
518 }
519
520 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
521 {
522 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
523 }
524
525 long wxTextCtrl::GetInsertionPoint() const
526 {
527 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
528
529 return (long) GTK_EDITABLE(m_text)->current_pos;
530 }
531
532 long wxTextCtrl::GetLastPosition() const
533 {
534 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
535
536 int pos = 0;
537 if (m_windowStyle & wxTE_MULTILINE)
538 pos = gtk_text_get_length( GTK_TEXT(m_text) );
539 else
540 pos = GTK_ENTRY(m_text)->text_length;
541
542 return (long)pos-1;
543 }
544
545 void wxTextCtrl::Remove( long from, long to )
546 {
547 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
548
549 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
550 }
551
552 void wxTextCtrl::Replace( long from, long to, const wxString &value )
553 {
554 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
555
556 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
557 if (value.IsNull()) return;
558 gint pos = (gint)to;
559 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
560 }
561
562 void wxTextCtrl::Cut()
563 {
564 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
565
566 #if (GTK_MINOR_VERSION == 1)
567 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
568 #else
569 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
570 #endif
571 }
572
573 void wxTextCtrl::Copy()
574 {
575 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
576
577 #if (GTK_MINOR_VERSION == 1)
578 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
579 #else
580 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
581 #endif
582 }
583
584 void wxTextCtrl::Paste()
585 {
586 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
587
588 #if (GTK_MINOR_VERSION == 1)
589 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
590 #else
591 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
592 #endif
593 }
594
595 void wxTextCtrl::Clear()
596 {
597 SetValue( "" );
598 }
599
600 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
601 {
602 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
603
604 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
605 {
606 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
607 event.SetEventObject(this);
608 if (GetEventHandler()->ProcessEvent(event)) return;
609 }
610 else if (key_event.KeyCode() == WXK_TAB)
611 {
612 wxNavigationKeyEvent event;
613 event.SetDirection( key_event.m_shiftDown );
614 event.SetWindowChange(FALSE);
615 event.SetEventObject(this);
616
617 if (GetEventHandler()->ProcessEvent(event)) return;
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