]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/textctrl.cpp
Removed some unnecessary bitmaps; other minor changes
[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 gint pos = 0;
307 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos );
308 }
309 else
310 {
311 gtk_entry_set_text( GTK_ENTRY(m_text), text );
312 }
313
314 free (text);
315 m_modified = FALSE;
316 return TRUE;
317 }
318 return FALSE;
319 }
320
321 bool wxTextCtrl::SaveFile( const wxString &file )
322 {
323 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
324
325 if (file == "") return FALSE;
326
327 FILE *fp;
328
329 if (!(fp = fopen ((char*) (const char*) file, "w")))
330 {
331 return FALSE;
332 }
333 else
334 {
335 char *text = NULL;
336 gint len = 0;
337
338 if (m_windowStyle & wxTE_MULTILINE)
339 {
340 len = gtk_text_get_length( GTK_TEXT(m_text) );
341 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
342 }
343 else
344 {
345 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
346 }
347
348 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
349 {
350 // Did not write whole file
351 }
352
353 // Make sure newline terminates the file
354 if (text[len - 1] != '\n')
355 fputc ('\n', fp);
356
357 fclose (fp);
358
359 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
360
361 m_modified = FALSE;
362 return TRUE;
363 }
364
365 return TRUE;
366 }
367
368 wxString wxTextCtrl::GetLineText( long lineNo ) const
369 {
370 if (m_windowStyle & wxTE_MULTILINE)
371 {
372 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
373 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
374
375 if (text)
376 {
377 wxString buf("");
378 long i;
379 int currentLine = 0;
380 for (i = 0; currentLine != lineNo && text[i]; i++ )
381 if (text[i] == '\n')
382 currentLine++;
383 // Now get the text
384 int j;
385 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
386 buf += text[i];
387
388 g_free( text );
389 return buf;
390 }
391 else
392 return wxEmptyString;
393 }
394 else
395 {
396 if (lineNo == 0) return GetValue();
397 return wxEmptyString;
398 }
399 }
400
401 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
402 {
403 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
404 }
405
406 long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
407 {
408 if (!(m_windowStyle & wxTE_MULTILINE))
409 return 0;
410 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
411 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
412 if(!text)
413 return 0;
414 if( pos >= len)
415 return pos=len-1;
416
417 *x=1; // Col 1
418 *y=1; // Line 1
419 for (int i = 0; i < pos; i++ )
420 {
421 if (text[i] == '\n')
422 {
423 (*y)++;
424 *x=1;
425 }
426 else
427 (*x)++;
428 }
429 g_free( text );
430 return 1;
431 }
432
433 long wxTextCtrl::XYToPosition(long x, long y ) const
434 {
435 if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
436
437 long pos=0;
438
439 for( int i=1; i<y; i++ ) pos += GetLineLength(i);
440
441 pos +=x-1; // Pos start with 0
442 return pos;
443 }
444
445 int wxTextCtrl::GetLineLength(long lineNo) const
446 {
447 wxString str = GetLineText (lineNo);
448 return (int) str.Length();
449 }
450
451 int wxTextCtrl::GetNumberOfLines() const
452 {
453 if (m_windowStyle & wxTE_MULTILINE)
454 {
455 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
456 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
457
458 if (text)
459 {
460 int currentLine = 0;
461 for (int i = 0; i < len; i++ )
462 {
463 if (text[i] == '\n')
464 currentLine++;
465 }
466 g_free( text );
467 return currentLine;
468 }
469 else
470 {
471 return 0;
472 }
473 }
474 else
475 {
476 return 1;
477 }
478 }
479
480 void wxTextCtrl::SetInsertionPoint( long pos )
481 {
482 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
483
484 int tmp = (int) pos;
485 if (m_windowStyle & wxTE_MULTILINE)
486 gtk_text_set_point( GTK_TEXT(m_text), tmp );
487 else
488 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
489 }
490
491 void wxTextCtrl::SetInsertionPointEnd()
492 {
493 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
494
495 int pos = 0;
496 if (m_windowStyle & wxTE_MULTILINE)
497 pos = gtk_text_get_length( GTK_TEXT(m_text) );
498 else
499 pos = GTK_ENTRY(m_text)->text_length;
500
501 SetInsertionPoint((pos-1)>0 ? (pos-1):0);
502 }
503
504 void wxTextCtrl::SetEditable( bool editable )
505 {
506 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
507
508 if (m_windowStyle & wxTE_MULTILINE)
509 gtk_text_set_editable( GTK_TEXT(m_text), editable );
510 else
511 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
512 }
513
514 void wxTextCtrl::SetSelection( long from, long to )
515 {
516 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
517
518 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
519 }
520
521 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
522 {
523 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
524 }
525
526 long wxTextCtrl::GetInsertionPoint() const
527 {
528 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
529
530 return (long) GTK_EDITABLE(m_text)->current_pos;
531 }
532
533 long wxTextCtrl::GetLastPosition() const
534 {
535 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
536
537 int pos = 0;
538 if (m_windowStyle & wxTE_MULTILINE)
539 pos = gtk_text_get_length( GTK_TEXT(m_text) );
540 else
541 pos = GTK_ENTRY(m_text)->text_length;
542
543 return (long)pos-1;
544 }
545
546 void wxTextCtrl::Remove( long from, long to )
547 {
548 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
549
550 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
551 }
552
553 void wxTextCtrl::Replace( long from, long to, const wxString &value )
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 if (value.IsNull()) return;
559 gint pos = (gint)from;
560 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
561 }
562
563 void wxTextCtrl::Cut()
564 {
565 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
566
567 #if (GTK_MINOR_VERSION == 1)
568 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
569 #else
570 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
571 #endif
572 }
573
574 void wxTextCtrl::Copy()
575 {
576 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
577
578 #if (GTK_MINOR_VERSION == 1)
579 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
580 #else
581 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
582 #endif
583 }
584
585 void wxTextCtrl::Paste()
586 {
587 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
588
589 #if (GTK_MINOR_VERSION == 1)
590 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
591 #else
592 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
593 #endif
594 }
595
596 void wxTextCtrl::Clear()
597 {
598 SetValue( "" );
599 }
600
601 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
602 {
603 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
604
605 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
606 {
607 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
608 event.SetEventObject(this);
609 if (GetEventHandler()->ProcessEvent(event)) return;
610 }
611 else if (key_event.KeyCode() == WXK_TAB)
612 {
613 wxNavigationKeyEvent event;
614 event.SetDirection( key_event.m_shiftDown );
615 event.SetWindowChange(FALSE);
616 event.SetEventObject(this);
617
618 if (GetEventHandler()->ProcessEvent(event)) return;
619 }
620 key_event.Skip();
621 }
622
623 int wxTextCtrl::overflow( int WXUNUSED(c) )
624 {
625 int len = pptr() - pbase();
626 char *txt = new char[len+1];
627 strncpy(txt, pbase(), len);
628 txt[len] = '\0';
629 (*this) << txt;
630 setp(pbase(), epptr());
631 delete[] txt;
632 return EOF;
633 }
634
635 int wxTextCtrl::sync()
636 {
637 int len = pptr() - pbase();
638 char *txt = new char[len+1];
639 strncpy(txt, pbase(), len);
640 txt[len] = '\0';
641 (*this) << txt;
642 setp(pbase(), epptr());
643 delete[] txt;
644 return 0;
645 }
646
647 int wxTextCtrl::underflow()
648 {
649 return EOF;
650 }
651
652 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
653 {
654 WriteText(s);
655 return *this;
656 }
657
658 wxTextCtrl& wxTextCtrl::operator<<(float f)
659 {
660 static char buf[100];
661 sprintf(buf, "%.2f", f);
662 WriteText(buf);
663 return *this;
664 }
665
666 wxTextCtrl& wxTextCtrl::operator<<(double d)
667 {
668 static char buf[100];
669 sprintf(buf, "%.2f", d);
670 WriteText(buf);
671 return *this;
672 }
673
674 wxTextCtrl& wxTextCtrl::operator<<(int i)
675 {
676 static char buf[100];
677 sprintf(buf, "%i", i);
678 WriteText(buf);
679 return *this;
680 }
681
682 wxTextCtrl& wxTextCtrl::operator<<(long i)
683 {
684 static char buf[100];
685 sprintf(buf, "%ld", i);
686 WriteText(buf);
687 return *this;
688 }
689
690 wxTextCtrl& wxTextCtrl::operator<<(const char c)
691 {
692 char buf[2];
693
694 buf[0] = c;
695 buf[1] = 0;
696 WriteText(buf);
697 return *this;
698 }
699
700 GtkWidget* wxTextCtrl::GetConnectWidget()
701 {
702 return GTK_WIDGET(m_text);
703 }
704
705 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
706 {
707 if (m_windowStyle & wxTE_MULTILINE)
708 return (window == GTK_TEXT(m_text)->text_area);
709 else
710 return (window == GTK_ENTRY(m_text)->text_area);
711 }
712
713 void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
714 {
715 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
716
717 // doesn't work
718 }
719
720 void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
721 {
722 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
723
724 // doesn't work
725 }
726
727 void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
728 {
729 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
730
731 wxControl::SetBackgroundColour( colour );
732
733 wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE );
734 if (sysbg.Red() == colour.Red() &&
735 sysbg.Green() == colour.Green() &&
736 sysbg.Blue() == colour.Blue())
737 {
738 return;
739 }
740
741 if (!m_backgroundColour.Ok()) return;
742
743 if (m_windowStyle & wxTE_MULTILINE)
744 {
745 GdkWindow *window = GTK_TEXT(m_text)->text_area;
746 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
747 gdk_window_set_background( window, m_backgroundColour.GetColor() );
748 gdk_window_clear( window );
749 }
750 }
751
752 void wxTextCtrl::ApplyWidgetStyle()
753 {
754 if (m_windowStyle & wxTE_MULTILINE)
755 {
756 // how ?
757 }
758 else
759 {
760 SetWidgetStyle();
761 gtk_widget_set_style( m_text, m_widgetStyle );
762 }
763 }
764