]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/textctrl.cpp
Added wxDC:DrawPolygone
[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
13289f04 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "textctrl.h"
13#endif
14
15#include "wx/textctrl.h"
16#include "wx/utils.h"
1a5a8367 17#include <wx/intl.h>
c801d85f
KB
18
19//-----------------------------------------------------------------------------
20// wxTextCtrl
21//-----------------------------------------------------------------------------
22
23IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
24
112892b9 25void gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win )
484e45bf 26{
9406d962 27 win->SetModified();
112892b9
RR
28};
29
c801d85f
KB
30
31BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
32// EVT_CHAR(wxTextCtrl::OnChar)
33END_EVENT_TABLE()
34
35wxTextCtrl::wxTextCtrl(void) : streambuf()
36{
e2414cbe 37 if( allocate() )
13289f04
VZ
38 setp(base(),ebuf());
39
112892b9 40 m_modified = FALSE;
c801d85f
KB
41};
42
debe6624 43wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 44 const wxPoint &pos, const wxSize &size,
debe6624 45 int style, const wxString &name ) : streambuf()
c801d85f 46{
13289f04
VZ
47 if( allocate() )
48 setp(base(),ebuf());
49
112892b9 50 m_modified = FALSE;
c801d85f
KB
51 Create( parent, id, value, pos, size, style, name );
52};
53
debe6624 54bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value,
484e45bf 55 const wxPoint &pos, const wxSize &size,
debe6624 56 int style, const wxString &name )
c801d85f
KB
57{
58 m_needParent = TRUE;
484e45bf 59
c801d85f 60 PreCreation( parent, id, pos, size, style, name );
484e45bf 61
13289f04 62 bool bMultiLine = (style & wxTE_MULTILINE) != 0;
47908e25
RR
63 if ( bMultiLine )
64 {
13289f04
VZ
65 // a multi-line edit control: create a vertical scrollbar by default and
66 // horizontal if requested
67 bool bHasHScrollbar = (style & wxHSCROLL) != 0;
68
69 // create our control...
70 m_text = gtk_text_new( NULL, NULL );
71
72 // ... and put into the upper left hand corner of the table
73 m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE);
74 gtk_table_attach(GTK_TABLE(m_widget), m_text, 0, 1, 0, 1,
75 GTK_FILL | GTK_EXPAND,
76 GTK_FILL | GTK_EXPAND | GTK_SHRINK,
77 0, 0);
78
79 // put the horizontal scrollbar in the lower left hand corner
80 if ( bHasHScrollbar ) {
81 GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj);
82 gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2,
83 GTK_EXPAND | GTK_FILL,
84 GTK_FILL,
85 0, 0);
86 gtk_widget_show(hscrollbar);
87 }
88
89 // finally, put the vertical scrollbar in the upper right corner
90 GtkWidget *vscrollbar = gtk_vscrollbar_new(GTK_TEXT(m_text)->vadj);
91 gtk_table_attach(GTK_TABLE(m_widget), vscrollbar, 1, 2, 0, 1,
92 GTK_FILL,
93 GTK_EXPAND | GTK_FILL | GTK_SHRINK,
94 0, 0);
95 gtk_widget_show(vscrollbar);
96 }
97 else {
98 // a single-line text control: no need for scrollbars
99 m_widget =
100 m_text = gtk_entry_new();
101 }
484e45bf 102
c801d85f
KB
103 wxSize newSize = size;
104 if (newSize.x == -1) newSize.x = 80;
105 if (newSize.y == -1) newSize.y = 26;
106 SetSize( newSize.x, newSize.y );
484e45bf 107
c801d85f 108 PostCreation();
484e45bf 109
13289f04
VZ
110 if ( bMultiLine ) {
111 gtk_widget_realize(m_text);
112 gtk_widget_show(m_text);
113 }
114
484e45bf 115 // we want to be notified about text changes
13289f04 116 gtk_signal_connect(GTK_OBJECT(m_text), "changed",
484e45bf
VZ
117 GTK_SIGNAL_FUNC(gtk_text_changed_callback),
118 (gpointer)this);
119
7f4dc78d
RR
120 if (!value.IsNull())
121 {
122 gint tmp = 0;
13289f04 123 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp );
112892b9 124 };
484e45bf 125
112892b9
RR
126 if (style & wxREADONLY)
127 {
128 }
129 else
130 {
13289f04
VZ
131 if ( bMultiLine )
132 gtk_text_set_editable( GTK_TEXT(m_text), 1 );
7f4dc78d 133 };
484e45bf 134
c801d85f 135 Show( TRUE );
484e45bf 136
c801d85f
KB
137 return TRUE;
138};
139
140wxString wxTextCtrl::GetValue(void) const
141{
142 wxString tmp;
143 if (m_windowStyle & wxTE_MULTILINE)
144 {
13289f04
VZ
145 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
146 tmp = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len );
c801d85f
KB
147 }
148 else
149 {
13289f04 150 tmp = gtk_entry_get_text( GTK_ENTRY(m_text) );
c801d85f
KB
151 };
152 return tmp;
153};
154
155void wxTextCtrl::SetValue( const wxString &value )
156{
157 wxString tmp = "";
158 if (!value.IsNull()) tmp = value;
159 if (m_windowStyle & wxTE_MULTILINE)
160 {
13289f04
VZ
161 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
162 gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len );
c801d85f 163 len = 0;
13289f04 164 gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len );
c801d85f
KB
165 }
166 else
167 {
13289f04 168 gtk_entry_set_text( GTK_ENTRY(m_text), tmp );
c801d85f
KB
169 };
170};
171
172void wxTextCtrl::WriteText( const wxString &text )
173{
174 if (text.IsNull()) return;
484e45bf 175
c801d85f
KB
176 if (m_windowStyle & wxTE_MULTILINE)
177 {
13289f04
VZ
178 gint len = gtk_text_get_length( GTK_TEXT(m_text) );
179 gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len );
c801d85f
KB
180 }
181 else
182 {
13289f04 183 gtk_entry_append_text( GTK_ENTRY(m_text), text );
c801d85f
KB
184 };
185};
186
112892b9 187bool wxTextCtrl::LoadFile( const wxString &WXUNUSED(file) )
c801d85f 188{
1a5a8367 189 wxFAIL_MSG(_("wxTextCtrl::LoadFile not implemented"));
2ad3a34e 190
112892b9 191 return FALSE;
c801d85f
KB
192};
193
112892b9 194bool wxTextCtrl::SaveFile( const wxString &WXUNUSED(file) )
c801d85f 195{
1a5a8367 196 wxFAIL_MSG(_("wxTextCtrl::SaveFile not implemented"));
2ad3a34e 197
112892b9 198 return FALSE;
c801d85f
KB
199};
200
112892b9 201/*
debe6624 202wxString wxTextCtrl::GetLineText( long lineNo ) const
c801d85f
KB
203{
204};
205
112892b9 206
c801d85f
KB
207void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
208{
209};
210
debe6624 211long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
c801d85f
KB
212{
213};
214
debe6624 215long wxTextCtrl::XYToPosition( long x, long y )
c801d85f
KB
216{
217};
218
219int wxTextCtrl::GetNumberOfLines(void)
220{
221};
222
223*/
debe6624 224void wxTextCtrl::SetInsertionPoint( long pos )
c801d85f
KB
225{
226 int tmp = (int) pos;
227 if (m_windowStyle & wxTE_MULTILINE)
13289f04 228 gtk_text_set_point( GTK_TEXT(m_text), tmp );
c801d85f 229 else
13289f04 230 gtk_entry_set_position( GTK_ENTRY(m_text), tmp );
c801d85f
KB
231};
232
233void wxTextCtrl::SetInsertionPointEnd(void)
234{
235 int pos = 0;
236 if (m_windowStyle & wxTE_MULTILINE)
13289f04 237 pos = gtk_text_get_length( GTK_TEXT(m_text) );
c801d85f 238 else
13289f04 239 pos = GTK_ENTRY(m_text)->text_length;
c801d85f
KB
240 SetInsertionPoint( pos-1 );
241};
242
debe6624 243void wxTextCtrl::SetEditable( bool editable )
c801d85f
KB
244{
245 if (m_windowStyle & wxTE_MULTILINE)
13289f04 246 gtk_text_set_editable( GTK_TEXT(m_text), editable );
c801d85f 247 else
13289f04 248 gtk_entry_set_editable( GTK_ENTRY(m_text), editable );
c801d85f
KB
249};
250
debe6624 251void wxTextCtrl::SetSelection( long from, long to )
c801d85f 252{
13289f04 253 gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to );
c801d85f
KB
254};
255
debe6624 256void wxTextCtrl::ShowPosition( long WXUNUSED(pos) )
c801d85f 257{
1a5a8367 258 wxFAIL_MSG(_("wxTextCtrl::ShowPosition not implemented"));
c801d85f
KB
259};
260
261long wxTextCtrl::GetInsertionPoint(void) const
262{
13289f04 263 return (long) GTK_EDITABLE(m_text)->current_pos;
c801d85f
KB
264};
265
266long wxTextCtrl::GetLastPosition(void) const
267{
268 int pos = 0;
269 if (m_windowStyle & wxTE_MULTILINE)
13289f04 270 pos = gtk_text_get_length( GTK_TEXT(m_text) );
c801d85f 271 else
13289f04 272 pos = GTK_ENTRY(m_text)->text_length;
c801d85f
KB
273 return (long)pos-1;
274};
275
debe6624 276void wxTextCtrl::Remove( long from, long to )
c801d85f 277{
13289f04 278 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
c801d85f
KB
279};
280
debe6624 281void wxTextCtrl::Replace( long from, long to, const wxString &value )
c801d85f 282{
13289f04 283 gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to );
c801d85f
KB
284 if (value.IsNull()) return;
285 gint pos = (gint)to;
13289f04 286 gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos );
c801d85f
KB
287};
288
289void wxTextCtrl::Cut(void)
290{
13289f04 291 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 );
c801d85f
KB
292};
293
294void wxTextCtrl::Copy(void)
295{
13289f04 296 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 );
c801d85f
KB
297};
298
299void wxTextCtrl::Paste(void)
300{
13289f04 301 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 );
c801d85f
KB
302};
303
304void wxTextCtrl::Delete(void)
305{
306 SetValue( "" );
307};
308
309void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) )
310{
311};
312
46dc76ba 313int wxTextCtrl::overflow( int WXUNUSED(c) )
c801d85f 314{
c801d85f
KB
315 int len = pptr() - pbase();
316 char *txt = new char[len+1];
317 strncpy(txt, pbase(), len);
318 txt[len] = '\0';
319 (*this) << txt;
320 setp(pbase(), epptr());
321 delete[] txt;
322 return EOF;
c801d85f
KB
323};
324
325int wxTextCtrl::sync(void)
326{
c801d85f
KB
327 int len = pptr() - pbase();
328 char *txt = new char[len+1];
329 strncpy(txt, pbase(), len);
330 txt[len] = '\0';
331 (*this) << txt;
332 setp(pbase(), epptr());
333 delete[] txt;
334 return 0;
c801d85f
KB
335};
336
337int wxTextCtrl::underflow(void)
338{
339 return EOF;
340};
341
342wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
343{
344 WriteText(s);
345 return *this;
346}
347
debe6624 348wxTextCtrl& wxTextCtrl::operator<<(float f)
c801d85f
KB
349{
350 static char buf[100];
351 sprintf(buf, "%.2f", f);
352 WriteText(buf);
353 return *this;
354}
355
debe6624 356wxTextCtrl& wxTextCtrl::operator<<(double d)
c801d85f
KB
357{
358 static char buf[100];
359 sprintf(buf, "%.2f", d);
360 WriteText(buf);
361 return *this;
362}
363
debe6624 364wxTextCtrl& wxTextCtrl::operator<<(int i)
c801d85f
KB
365{
366 static char buf[100];
367 sprintf(buf, "%i", i);
368 WriteText(buf);
369 return *this;
370}
371
debe6624 372wxTextCtrl& wxTextCtrl::operator<<(long i)
c801d85f
KB
373{
374 static char buf[100];
375 sprintf(buf, "%ld", i);
376 WriteText(buf);
377 return *this;
378}
379
380wxTextCtrl& wxTextCtrl::operator<<(const char c)
381{
382 char buf[2];
383
384 buf[0] = c;
385 buf[1] = 0;
386 WriteText(buf);
387 return *this;
388}
389
e3e65dac
RR
390GtkWidget* wxTextCtrl::GetDropTargetWidget(void)
391{
392 return GTK_WIDGET(m_text);
393};
394
395
396
397