]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
New toolbar event processing
[wxWidgets.git] / src / gtk / textctrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "textctrl.h"
13#endif
14
15#include "wx/textctrl.h"
16#include "wx/utils.h"
17
18//-----------------------------------------------------------------------------
19// wxTextCtrl
20//-----------------------------------------------------------------------------
21
22IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
23
112892b9 24void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 25{
9406d962 26 win->SetModified();
112892b9
RR
27};
28
c801d85f
KB
29
30BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
31// EVT_CHAR(wxTextCtrl::OnChar)
32END_EVENT_TABLE()
33
34wxTextCtrl::wxTextCtrl(void) : streambuf()
35{
e2414cbe
RR
36 if( allocate() )
37 setp(base(),ebuf());
38
112892b9 39 m_modified = FALSE;
c801d85f
KB
40};
41
debe6624 42wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 43 const wxPoint &pos, const wxSize &size,
debe6624 44 int style, const wxString &name ) : streambuf()
c801d85f 45{
e2414cbe
RR
46 if( allocate() )
47 setp(base(),ebuf());
48
112892b9 49 m_modified = FALSE;
c801d85f
KB
50 Create( parent, id, value, pos, size, style, name );
51};
52
debe6624 53bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 54 const wxPoint &pos, const wxSize &size,
debe6624 55 int style, const wxString &name )
c801d85f
KB
56{
57 m_needParent = TRUE;
484e45bf 58
c801d85f 59 PreCreation( parent, id, pos, size, style, name );
484e45bf 60
c801d85f
KB
61 if (style & wxTE_MULTILINE)
62 m_widget = gtk_text_new( NULL, NULL );
63 else
64 m_widget = gtk_entry_new();
484e45bf 65
c801d85f
KB
66 wxSize newSize = size;
67 if (newSize.x == -1) newSize.x = 80;
68 if (newSize.y == -1) newSize.y = 26;
69 SetSize( newSize.x, newSize.y );
484e45bf 70
c801d85f 71 PostCreation();
484e45bf
VZ
72
73 // we want to be notified about text changes
74 gtk_signal_connect(GTK_OBJECT(m_widget), "changed",
75 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
76 (gpointer)this);
77
7f4dc78d
RR
78 if (!value.IsNull())
79 {
80 gint tmp = 0;
484e45bf 81 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &tmp );
112892b9 82 };
484e45bf 83
112892b9
RR
84 if (style & wxREADONLY)
85 {
86 }
87 else
88 {
89 if (style & wxTE_MULTILINE) gtk_text_set_editable( GTK_TEXT(m_widget), 1 );
7f4dc78d 90 };
484e45bf 91
c801d85f 92 Show( TRUE );
484e45bf 93
c801d85f
KB
94 return TRUE;
95};
96
97wxString wxTextCtrl::GetValue(void) const
98{
99 wxString tmp;
100 if (m_windowStyle & wxTE_MULTILINE)
101 {
102 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
484e45bf 103 tmp = gtk_editable_get_chars( GTK_EDITABLE(m_widget), 0, len );
c801d85f
KB
104 }
105 else
106 {
107 tmp = gtk_entry_get_text( GTK_ENTRY(m_widget) );
108 };
109 return tmp;
110};
111
112void wxTextCtrl::SetValue( const wxString &value )
113{
114 wxString tmp = "";
115 if (!value.IsNull()) tmp = value;
116 if (m_windowStyle & wxTE_MULTILINE)
117 {
118 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
484e45bf 119 gtk_editable_delete_text( GTK_EDITABLE(m_widget), 0, len );
c801d85f
KB
120 len = 0;
121 gtk_editable_insert_text( GTK_EDITABLE(m_widget), tmp, tmp.Length(), &len );
122 }
123 else
124 {
125 gtk_entry_set_text( GTK_ENTRY(m_widget), tmp );
126 };
127};
128
129void wxTextCtrl::WriteText( const wxString &text )
130{
131 if (text.IsNull()) return;
484e45bf 132
c801d85f
KB
133 if (m_windowStyle & wxTE_MULTILINE)
134 {
2ad3a34e 135 gint len = gtk_text_get_length( GTK_TEXT(m_widget) );
c801d85f
KB
136 gtk_editable_insert_text( GTK_EDITABLE(m_widget), text, text.Length(), &len );
137 }
138 else
139 {
140 gtk_entry_append_text( GTK_ENTRY(m_widget), text );
141 };
142};
143
112892b9 144bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) )
c801d85f 145{
2ad3a34e
VZ
146 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
147
112892b9 148 return FALSE;
c801d85f
KB
149};
150
112892b9 151bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) )
c801d85f 152{
2ad3a34e
VZ
153 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
154
112892b9 155 return FALSE;
c801d85f
KB
156};
157
112892b9 158/*
debe6624 159wxString wxTextCtrl::GetLineText( long lineNo ) const
c801d85f
KB
160{
161};
162
112892b9 163
c801d85f
KB
164void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
165{
166};
167
debe6624 168long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
c801d85f
KB
169{
170};
171
debe6624 172long wxTextCtrl::XYToPosition( long x, long y )
c801d85f
KB
173{
174};
175
176int wxTextCtrl::GetNumberOfLines(void)
177{
178};
179
180*/
debe6624 181void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f
KB
182{
183 int tmp = (int) pos;
184 if (m_windowStyle & wxTE_MULTILINE)
185 gtk_text_set_point( GTK_TEXT(m_widget), tmp );
186 else
187 gtk_entry_set_position( GTK_ENTRY(m_widget), tmp );
188};
189
190void wxTextCtrl::SetInsertionPointEnd(void)
191{
192 int pos = 0;
193 if (m_windowStyle & wxTE_MULTILINE)
194 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
195 else
196 pos = GTK_ENTRY(m_widget)->text_length;
197 SetInsertionPoint( pos-1 );
198};
199
debe6624 200void wxTextCtrl::SetEditable( bool editable )
c801d85f
KB
201{
202 if (m_windowStyle & wxTE_MULTILINE)
203 gtk_text_set_editable( GTK_TEXT(m_widget), editable );
204 else
205 gtk_entry_set_editable( GTK_ENTRY(m_widget), editable );
206};
207
debe6624 208void wxTextCtrl::SetSelection( long from, long to )
c801d85f
KB
209{
210 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
211};
212
debe6624 213void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 214{
2ad3a34e 215 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
c801d85f
KB
216};
217
218long wxTextCtrl::GetInsertionPoint(void) const
219{
220 return (long) GTK_EDITABLE(m_widget)->current_pos;
221};
222
223long wxTextCtrl::GetLastPosition(void) const
224{
225 int pos = 0;
226 if (m_windowStyle & wxTE_MULTILINE)
227 pos = gtk_text_get_length( GTK_TEXT(m_widget) );
228 else
229 pos = GTK_ENTRY(m_widget)->text_length;
230 return (long)pos-1;
231};
232
debe6624 233void wxTextCtrl::Remove( long from, long to )
c801d85f
KB
234{
235 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
236};
237
debe6624 238void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f
KB
239{
240 gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
241 if (value.IsNull()) return;
242 gint pos = (gint)to;
243 gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &pos );
244};
245
246void wxTextCtrl::Cut(void)
247{
248 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget), 0 );
249};
250
251void wxTextCtrl::Copy(void)
252{
253 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget), 0 );
254};
255
256void wxTextCtrl::Paste(void)
257{
258 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget), 0 );
259};
260
261void wxTextCtrl::Delete(void)
262{
263 SetValue( "" );
264};
265
266void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) )
267{
268};
269
270int wxTextCtrl::overflow(int c)
271{
c801d85f
KB
272 int len = pptr() - pbase();
273 char *txt = new char[len+1];
274 strncpy(txt, pbase(), len);
275 txt[len] = '\0';
276 (*this) << txt;
277 setp(pbase(), epptr());
278 delete[] txt;
279 return EOF;
c801d85f
KB
280};
281
282int wxTextCtrl::sync(void)
283{
c801d85f
KB
284 int len = pptr() - pbase();
285 char *txt = new char[len+1];
286 strncpy(txt, pbase(), len);
287 txt[len] = '\0';
288 (*this) << txt;
289 setp(pbase(), epptr());
290 delete[] txt;
291 return 0;
c801d85f
KB
292};
293
294int wxTextCtrl::underflow(void)
295{
296 return EOF;
297};
298
299wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
300{
301 WriteText(s);
302 return *this;
303}
304
debe6624 305wxTextCtrl& wxTextCtrl::operator<<(float f)
c801d85f
KB
306{
307 static char buf[100];
308 sprintf(buf, "%.2f", f);
309 WriteText(buf);
310 return *this;
311}
312
debe6624 313wxTextCtrl& wxTextCtrl::operator<<(double d)
c801d85f
KB
314{
315 static char buf[100];
316 sprintf(buf, "%.2f", d);
317 WriteText(buf);
318 return *this;
319}
320
debe6624 321wxTextCtrl& wxTextCtrl::operator<<(int i)
c801d85f
KB
322{
323 static char buf[100];
324 sprintf(buf, "%i", i);
325 WriteText(buf);
326 return *this;
327}
328
debe6624 329wxTextCtrl& wxTextCtrl::operator<<(long i)
c801d85f
KB
330{
331 static char buf[100];
332 sprintf(buf, "%ld", i);
333 WriteText(buf);
334 return *this;
335}
336
337wxTextCtrl& wxTextCtrl::operator<<(const char c)
338{
339 char buf[2];
340
341 buf[0] = c;
342 buf[1] = 0;
343 WriteText(buf);
344 return *this;
345}
346