]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/textctrl.cpp
Fixed module code
[wxWidgets.git] / src / gtk / 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 bMultiLine = (style & wxTE_MULTILINE) != 0;
75 if ( bMultiLine )
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 (bMultiLine)
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_READONLY)
147 {
148 }
149 else
150 {
151 if (bMultiLine)
152 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
153 }
154
155 Show( TRUE );
156
157 SetBackgroundColour( parent->GetBackgroundColour() );
158 SetForegroundColour( parent->GetForegroundColour() );
159
160 return TRUE;
161 }
162
163 wxString wxTextCtrl::GetValue() const
164 {
165 wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" );
166
167 wxString tmp;
168 if (m_windowStyle & wxTE_MULTILINE)
169 {
170 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
171 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
172 tmp = text;
173 g_free( text );
174 }
175 else
176 {
177 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
178 }
179 return tmp;
180 }
181
182 void wxTextCtrl::SetValue( const wxString &value )
183 {
184 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
185
186 wxString tmp = "";
187 if (!value.IsNull()) tmp = value;
188 if (m_windowStyle & wxTE_MULTILINE)
189 {
190 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
191 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
192 len = 0;
193 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
194 }
195 else
196 {
197 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
198 }
199 }
200
201 void wxTextCtrl::WriteText( const wxString &text )
202 {
203 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
204
205 if (text.IsNull()) return;
206
207 if (m_windowStyle & wxTE_MULTILINE)
208 {
209 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
210 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
211 }
212 else
213 {
214 gtk_entry_append_text( GTK_ENTRY(m_text), text );
215 }
216 }
217
218 bool wxTextCtrl::LoadFile( const wxString &file )
219 {
220 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
221
222 if (!wxFileExists(file)) return FALSE;
223
224 Clear();
225
226 FILE *fp = NULL;
227 struct stat statb;
228
229 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
230 !(fp = fopen ((char*) (const char*) file, "r")))
231 {
232 return FALSE;
233 }
234 else
235 {
236 gint len = statb.st_size;
237 char *text;
238 if (!(text = (char*)malloc ((unsigned) (len + 1))))
239 {
240 fclose (fp);
241 return FALSE;
242 }
243 if (fread (text, sizeof (char), len, fp) != (size_t) len)
244 {
245 }
246 fclose (fp);
247
248 text[len] = 0;
249
250 if (m_windowStyle & wxTE_MULTILINE)
251 {
252 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, 0, &len );
253 }
254 else
255 {
256 gtk_entry_set_text( GTK_ENTRY(m_text), text );
257 }
258
259 free (text);
260 m_modified = FALSE;
261 return TRUE;
262 }
263 return FALSE;
264 }
265
266 bool wxTextCtrl::SaveFile( const wxString &file )
267 {
268 wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" );
269
270 if (file == "") return FALSE;
271
272 FILE *fp;
273
274 if (!(fp = fopen ((char*) (const char*) file, "w")))
275 {
276 return FALSE;
277 }
278 else
279 {
280 char *text = NULL;
281 gint len = 0;
282
283 if (m_windowStyle & wxTE_MULTILINE)
284 {
285 len = gtk_text_get_length( GTK_TEXT(m_text) );
286 text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
287 }
288 else
289 {
290 text = gtk_entry_get_text( GTK_ENTRY(m_text) );
291 }
292
293 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
294 {
295 // Did not write whole file
296 }
297
298 // Make sure newline terminates the file
299 if (text[len - 1] != '\n')
300 fputc ('\n', fp);
301
302 fclose (fp);
303
304 if (m_windowStyle & wxTE_MULTILINE) g_free( text );
305
306 m_modified = FALSE;
307 return TRUE;
308 }
309
310 return TRUE;
311 }
312
313 wxString wxTextCtrl::GetLineText( long lineNo ) const
314 {
315 if (m_windowStyle & wxTE_MULTILINE)
316 {
317 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
318 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
319
320 if (text)
321 {
322 wxString buf("");
323 long i;
324 int currentLine = 0;
325 for (i = 0; currentLine != lineNo && text[i]; i++ )
326 if (text[i] == '\n')
327 currentLine++;
328 // Now get the text
329 int j;
330 for (j = 0; text[i] && text[i] != '\n'; i++, j++ )
331 buf += text[i];
332
333 g_free( text );
334 return buf;
335 }
336 else
337 return wxEmptyString;
338 }
339 else
340 {
341 if (lineNo == 0) return GetValue();
342 return wxEmptyString;
343 }
344 }
345
346 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) )
347 {
348 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
349 }
350
351 long wxTextCtrl::PositionToXY( long WXUNUSED(pos), long *WXUNUSED(x), long *WXUNUSED(y) ) const
352 {
353 wxFAIL_MSG( "wxTextCtrl::XYToPosition not implemented" );
354
355 return 0;
356 }
357
358 long wxTextCtrl::XYToPosition( long WXUNUSED(x), long WXUNUSED(y) ) const
359 {
360 wxFAIL_MSG( "wxTextCtrl::XYToPosition not implemented" );
361
362 return 0;
363 }
364
365 int wxTextCtrl::GetLineLength(long lineNo) const
366 {
367 wxString str = GetLineText (lineNo);
368 return (int) str.Length();
369 }
370
371 int wxTextCtrl::GetNumberOfLines() const
372 {
373 if (m_windowStyle & wxTE_MULTILINE)
374 {
375 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
376 char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
377
378 if (text)
379 {
380 int currentLine = 0;
381 for (int i = 0; i < len; i++ )
382 if (text[i] == '\n')
383 currentLine++;
384
385 g_free( text );
386 return currentLine;
387 }
388 else
389 return 0;
390 }
391 else
392 {
393 return 1;
394 }
395 }
396
397 void wxTextCtrl::SetInsertionPoint( long pos )
398 {
399 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
400
401 int tmp = (int) pos;
402 if (m_windowStyle & wxTE_MULTILINE)
403 gtk_text_set_point( GTK_TEXT(m_text), tmp );
404 else
405 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
406 }
407
408 void wxTextCtrl::SetInsertionPointEnd()
409 {
410 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
411
412 int pos = 0;
413 if (m_windowStyle & wxTE_MULTILINE)
414 pos = gtk_text_get_length( GTK_TEXT(m_text) );
415 else
416 pos = GTK_ENTRY(m_text)->text_length;
417 SetInsertionPoint((pos-1)>0 ? (pos-1):0);
418 }
419
420 void wxTextCtrl::SetEditable( bool editable )
421 {
422 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
423
424 if (m_windowStyle & wxTE_MULTILINE)
425 gtk_text_set_editable( GTK_TEXT(m_text), editable );
426 else
427 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
428 }
429
430 void wxTextCtrl::SetSelection( long from, long to )
431 {
432 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
433
434 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
435 }
436
437 void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
438 {
439 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
440 }
441
442 long wxTextCtrl::GetInsertionPoint() const
443 {
444 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
445
446 return (long) GTK_EDITABLE(m_text)->current_pos;
447 }
448
449 long wxTextCtrl::GetLastPosition() const
450 {
451 wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" );
452
453 int pos = 0;
454 if (m_windowStyle & wxTE_MULTILINE)
455 pos = gtk_text_get_length( GTK_TEXT(m_text) );
456 else
457 pos = GTK_ENTRY(m_text)->text_length;
458 return (long)pos-1;
459 }
460
461 void wxTextCtrl::Remove( long from, long to )
462 {
463 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
464
465 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
466 }
467
468 void wxTextCtrl::Replace( long from, long to, const wxString &value )
469 {
470 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
471
472 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
473 if (value.IsNull()) return;
474 gint pos = (gint)to;
475 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
476 }
477
478 void wxTextCtrl::Cut()
479 {
480 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
481
482 #if (GTK_MINOR_VERSION == 1)
483 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) );
484 #else
485 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
486 #endif
487 }
488
489 void wxTextCtrl::Copy()
490 {
491 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
492
493 #if (GTK_MINOR_VERSION == 1)
494 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) );
495 #else
496 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
497 #endif
498 }
499
500 void wxTextCtrl::Paste()
501 {
502 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
503
504 #if (GTK_MINOR_VERSION == 1)
505 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) );
506 #else
507 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
508 #endif
509 }
510
511 void wxTextCtrl::Clear()
512 {
513 SetValue( "" );
514 }
515
516 void wxTextCtrl::OnChar( wxKeyEvent &key_event )
517 {
518 if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER))
519 {
520 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
521 event.SetEventObject(this);
522 if (GetEventHandler()->ProcessEvent(event)) return;
523 }
524 else if (key_event.KeyCode() == WXK_TAB)
525 {
526 wxNavigationKeyEvent event;
527 event.SetDirection( key_event.m_shiftDown );
528 event.SetWindowChange(FALSE);
529 event.SetEventObject(this);
530
531 if (GetEventHandler()->ProcessEvent(event)) return;
532 }
533 key_event.Skip();
534 }
535
536 int wxTextCtrl::overflow( int WXUNUSED(c) )
537 {
538 int len = pptr() - pbase();
539 char *txt = new char[len+1];
540 strncpy(txt, pbase(), len);
541 txt[len] = '\0';
542 (*this) << txt;
543 setp(pbase(), epptr());
544 delete[] txt;
545 return EOF;
546 }
547
548 int wxTextCtrl::sync()
549 {
550 int len = pptr() - pbase();
551 char *txt = new char[len+1];
552 strncpy(txt, pbase(), len);
553 txt[len] = '\0';
554 (*this) << txt;
555 setp(pbase(), epptr());
556 delete[] txt;
557 return 0;
558 }
559
560 int wxTextCtrl::underflow()
561 {
562 return EOF;
563 }
564
565 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
566 {
567 WriteText(s);
568 return *this;
569 }
570
571 wxTextCtrl& wxTextCtrl::operator<<(float f)
572 {
573 static char buf[100];
574 sprintf(buf, "%.2f", f);
575 WriteText(buf);
576 return *this;
577 }
578
579 wxTextCtrl& wxTextCtrl::operator<<(double d)
580 {
581 static char buf[100];
582 sprintf(buf, "%.2f", d);
583 WriteText(buf);
584 return *this;
585 }
586
587 wxTextCtrl& wxTextCtrl::operator<<(int i)
588 {
589 static char buf[100];
590 sprintf(buf, "%i", i);
591 WriteText(buf);
592 return *this;
593 }
594
595 wxTextCtrl& wxTextCtrl::operator<<(long i)
596 {
597 static char buf[100];
598 sprintf(buf, "%ld", i);
599 WriteText(buf);
600 return *this;
601 }
602
603 wxTextCtrl& wxTextCtrl::operator<<(const char c)
604 {
605 char buf[2];
606
607 buf[0] = c;
608 buf[1] = 0;
609 WriteText(buf);
610 return *this;
611 }
612
613 GtkWidget* wxTextCtrl::GetConnectWidget()
614 {
615 return GTK_WIDGET(m_text);
616 }
617
618 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window )
619 {
620 if (m_windowStyle & wxTE_MULTILINE)
621 return (window == GTK_TEXT(m_text)->text_area);
622 else
623 return (window == GTK_ENTRY(m_text)->text_area);
624 }
625
626 void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) )
627 {
628 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
629
630 // doesn't work
631 }
632
633 void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) )
634 {
635 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
636
637 // doesn't work
638 }
639
640 void wxTextCtrl::SetBackgroundColour( const wxColour &colour )
641 {
642 wxCHECK_RET( m_text != NULL, "invalid text ctrl" );
643
644 wxControl::SetBackgroundColour( colour );
645
646 if (!m_backgroundColour.Ok()) return;
647
648 if (m_windowStyle & wxTE_MULTILINE)
649 {
650 GdkWindow *window = GTK_TEXT(m_text)->text_area;
651 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
652 gdk_window_set_background( window, m_backgroundColour.GetColor() );
653 gdk_window_clear( window );
654 }
655 }
656
657 void wxTextCtrl::ApplyWidgetStyle()
658 {
659 if (m_windowStyle & wxTE_MULTILINE)
660 {
661 }
662 else
663 {
664 SetWidgetStyle();
665 gtk_widget_set_style( m_text, m_widgetStyle );
666 }
667 }
668