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