]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin |
13289f04 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "textctrl.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/textctrl.h" | |
15 | #include "wx/utils.h" | |
ae0bdb01 RR |
16 | #include "wx/intl.h" |
17 | #include "wx/settings.h" | |
c801d85f | 18 | |
a81258be RR |
19 | #include <sys/types.h> |
20 | #include <sys/stat.h> | |
21 | #include <ctype.h> | |
22 | ||
83624f79 RR |
23 | #include "gdk/gdk.h" |
24 | #include "gtk/gtk.h" | |
b292e2f5 RR |
25 | #include "gdk/gdkkeysyms.h" |
26 | ||
27 | //----------------------------------------------------------------------------- | |
28 | // data | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | extern bool g_blockEventsOnDrag; | |
32 | ||
c801d85f | 33 | //----------------------------------------------------------------------------- |
2f2aa628 | 34 | // "changed" |
c801d85f KB |
35 | //----------------------------------------------------------------------------- |
36 | ||
805dd538 | 37 | static void |
2830bf19 | 38 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
484e45bf | 39 | { |
2830bf19 | 40 | win->SetModified(); |
805dd538 | 41 | |
2830bf19 | 42 | win->CalculateScrollbar(); |
8bbe427f | 43 | |
2830bf19 | 44 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId ); |
0c77152e | 45 | event.SetString( copystring(win->GetValue()) ); |
2830bf19 RR |
46 | event.SetEventObject( win ); |
47 | win->GetEventHandler()->ProcessEvent( event ); | |
0c77152e | 48 | delete[] event.GetString(); |
6de97a3b | 49 | } |
112892b9 | 50 | |
2830bf19 RR |
51 | //----------------------------------------------------------------------------- |
52 | // "size_allocate" | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
805dd538 | 55 | static void |
2830bf19 RR |
56 | gtk_text_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* WXUNUSED(alloc), wxTextCtrl *win ) |
57 | { | |
58 | win->CalculateScrollbar(); | |
59 | } | |
60 | ||
2f2aa628 RR |
61 | //----------------------------------------------------------------------------- |
62 | // wxTextCtrl | |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
66 | ||
c801d85f | 67 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
2830bf19 | 68 | EVT_CHAR(wxTextCtrl::OnChar) |
c801d85f KB |
69 | END_EVENT_TABLE() |
70 | ||
f5abe911 | 71 | #ifndef NO_TEXT_WINDOW_STREAM |
03f38c58 | 72 | wxTextCtrl::wxTextCtrl() : streambuf() |
c801d85f | 73 | { |
2830bf19 | 74 | if (allocate()) setp(base(),ebuf()); |
13289f04 | 75 | |
2830bf19 | 76 | m_modified = FALSE; |
6de97a3b | 77 | } |
f5abe911 RR |
78 | #else |
79 | wxTextCtrl::wxTextCtrl() | |
80 | { | |
81 | m_modified = FALSE; | |
82 | } | |
83 | #endif | |
c801d85f | 84 | |
f5abe911 | 85 | #ifndef NO_TEXT_WINDOW_STREAM |
debe6624 | 86 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 87 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 88 | int style, const wxValidator& validator, const wxString &name ) : streambuf() |
c801d85f | 89 | { |
2830bf19 | 90 | if (allocate()) setp(base(),ebuf()); |
13289f04 | 91 | |
2830bf19 RR |
92 | m_modified = FALSE; |
93 | Create( parent, id, value, pos, size, style, validator, name ); | |
6de97a3b | 94 | } |
f5abe911 RR |
95 | #else |
96 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, | |
97 | const wxPoint &pos, const wxSize &size, | |
98 | int style, const wxValidator& validator, const wxString &name ) | |
99 | { | |
100 | m_modified = FALSE; | |
101 | Create( parent, id, value, pos, size, style, validator, name ); | |
102 | } | |
103 | #endif | |
c801d85f | 104 | |
debe6624 | 105 | bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 106 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 107 | int style, const wxValidator& validator, const wxString &name ) |
c801d85f | 108 | { |
2830bf19 | 109 | m_needParent = TRUE; |
b292e2f5 | 110 | m_acceptsFocus = TRUE; |
484e45bf | 111 | |
2830bf19 | 112 | PreCreation( parent, id, pos, size, style, name ); |
6de97a3b | 113 | |
2830bf19 | 114 | SetValidator( validator ); |
805dd538 | 115 | |
2830bf19 | 116 | m_vScrollbarVisible = TRUE; |
13289f04 | 117 | |
2830bf19 RR |
118 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
119 | if ( multi_line ) | |
120 | { | |
121 | // a multi-line edit control: create a vertical scrollbar by default and | |
122 | // horizontal if requested | |
123 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; | |
805dd538 | 124 | |
2830bf19 RR |
125 | // create our control... |
126 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); | |
127 | ||
128 | // ... and put into the upper left hand corner of the table | |
129 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); | |
130 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, | |
41dee9d0 | 131 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
f5368809 RR |
132 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
133 | 0, 0); | |
13289f04 | 134 | |
2830bf19 RR |
135 | // put the horizontal scrollbar in the lower left hand corner |
136 | if (bHasHScrollbar) | |
137 | { | |
138 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); | |
139 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, | |
f5368809 | 140 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), |
13289f04 VZ |
141 | GTK_FILL, |
142 | 0, 0); | |
2830bf19 RR |
143 | gtk_widget_show(hscrollbar); |
144 | } | |
13289f04 | 145 | |
2830bf19 RR |
146 | // finally, put the vertical scrollbar in the upper right corner |
147 | m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj ); | |
148 | gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1, | |
13289f04 | 149 | GTK_FILL, |
f5368809 | 150 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), |
13289f04 | 151 | 0, 0); |
2830bf19 | 152 | gtk_widget_show( m_vScrollbar ); |
805dd538 | 153 | |
2830bf19 RR |
154 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
155 | GTK_SIGNAL_FUNC(gtk_text_size_callback), (gpointer)this ); | |
156 | } | |
157 | else | |
158 | { | |
159 | // a single-line text control: no need for scrollbars | |
160 | m_widget = | |
161 | m_text = gtk_entry_new(); | |
162 | } | |
484e45bf | 163 | |
2830bf19 RR |
164 | wxSize newSize = size; |
165 | if (newSize.x == -1) newSize.x = 80; | |
166 | if (newSize.y == -1) newSize.y = 26; | |
167 | SetSize( newSize.x, newSize.y ); | |
484e45bf | 168 | |
2830bf19 | 169 | m_parent->AddChild( this ); |
6ca41e57 | 170 | |
2830bf19 | 171 | (m_parent->m_insertCallback)( m_parent, this ); |
8bbe427f | 172 | |
2830bf19 | 173 | PostCreation(); |
484e45bf | 174 | |
2830bf19 RR |
175 | if (multi_line) |
176 | { | |
177 | gtk_widget_realize(m_text); | |
178 | gtk_widget_show(m_text); | |
179 | } | |
13289f04 | 180 | |
2830bf19 | 181 | // we want to be notified about text changes |
b292e2f5 RR |
182 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
183 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
484e45bf | 184 | |
2830bf19 RR |
185 | if (!value.IsNull()) |
186 | { | |
187 | gint tmp = 0; | |
188 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); | |
189 | SetInsertionPointEnd(); | |
190 | } | |
484e45bf | 191 | |
2830bf19 RR |
192 | if (style & wxTE_PASSWORD) |
193 | { | |
194 | if (!multi_line) | |
195 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
196 | } | |
8bbe427f | 197 | |
2830bf19 RR |
198 | if (style & wxTE_READONLY) |
199 | { | |
200 | if (!multi_line) | |
201 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
202 | } | |
203 | else | |
204 | { | |
205 | if (multi_line) | |
206 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
207 | } | |
805dd538 | 208 | |
2830bf19 | 209 | Show( TRUE ); |
805dd538 | 210 | |
2830bf19 RR |
211 | SetBackgroundColour( parent->GetBackgroundColour() ); |
212 | SetForegroundColour( parent->GetForegroundColour() ); | |
484e45bf | 213 | |
2830bf19 RR |
214 | return TRUE; |
215 | } | |
484e45bf | 216 | |
2830bf19 RR |
217 | void wxTextCtrl::CalculateScrollbar() |
218 | { | |
219 | if ((m_windowStyle & wxTE_MULTILINE) == 0) return; | |
f96aa4d9 | 220 | |
2830bf19 | 221 | GtkAdjustment *adj = GTK_TEXT(m_text)->vadj; |
805dd538 | 222 | |
2830bf19 RR |
223 | if (adj->upper - adj->page_size < 0.8) |
224 | { | |
225 | if (m_vScrollbarVisible) | |
226 | { | |
805dd538 VZ |
227 | gtk_widget_hide( m_vScrollbar ); |
228 | ||
229 | m_vScrollbarVisible = FALSE; | |
2830bf19 RR |
230 | } |
231 | } | |
232 | else | |
233 | { | |
234 | if (!m_vScrollbarVisible) | |
235 | { | |
805dd538 VZ |
236 | gtk_widget_show( m_vScrollbar ); |
237 | ||
238 | m_vScrollbarVisible = TRUE; | |
2830bf19 RR |
239 | } |
240 | } | |
6de97a3b | 241 | } |
c801d85f | 242 | |
03f38c58 | 243 | wxString wxTextCtrl::GetValue() const |
c801d85f | 244 | { |
2830bf19 | 245 | wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" ); |
8bbe427f | 246 | |
2830bf19 RR |
247 | wxString tmp; |
248 | if (m_windowStyle & wxTE_MULTILINE) | |
249 | { | |
250 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
251 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
252 | tmp = text; | |
253 | g_free( text ); | |
254 | } | |
255 | else | |
256 | { | |
257 | tmp = gtk_entry_get_text( GTK_ENTRY(m_text) ); | |
258 | } | |
259 | return tmp; | |
6de97a3b | 260 | } |
c801d85f KB |
261 | |
262 | void wxTextCtrl::SetValue( const wxString &value ) | |
263 | { | |
2830bf19 | 264 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 265 | |
2830bf19 RR |
266 | wxString tmp = ""; |
267 | if (!value.IsNull()) tmp = value; | |
268 | if (m_windowStyle & wxTE_MULTILINE) | |
269 | { | |
270 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
271 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
272 | len = 0; | |
273 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len ); | |
274 | } | |
275 | else | |
276 | { | |
277 | gtk_entry_set_text( GTK_ENTRY(m_text), tmp ); | |
278 | } | |
6de97a3b | 279 | } |
c801d85f KB |
280 | |
281 | void wxTextCtrl::WriteText( const wxString &text ) | |
282 | { | |
2830bf19 | 283 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 284 | |
2830bf19 | 285 | if (text.IsNull()) return; |
484e45bf | 286 | |
2830bf19 RR |
287 | if (m_windowStyle & wxTE_MULTILINE) |
288 | { | |
289 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
290 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); | |
291 | } | |
292 | else | |
293 | { | |
294 | gtk_entry_append_text( GTK_ENTRY(m_text), text ); | |
295 | } | |
6de97a3b | 296 | } |
c801d85f | 297 | |
a81258be | 298 | bool wxTextCtrl::LoadFile( const wxString &file ) |
c801d85f | 299 | { |
a81258be | 300 | wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" ); |
8bbe427f | 301 | |
a81258be | 302 | if (!wxFileExists(file)) return FALSE; |
2ad3a34e | 303 | |
a81258be RR |
304 | Clear(); |
305 | ||
bbe0af5b | 306 | FILE *fp = (FILE*) NULL; |
a81258be | 307 | struct stat statb; |
8bbe427f | 308 | |
a81258be RR |
309 | if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || |
310 | !(fp = fopen ((char*) (const char*) file, "r"))) | |
311 | { | |
312 | return FALSE; | |
313 | } | |
314 | else | |
315 | { | |
316 | gint len = statb.st_size; | |
317 | char *text; | |
318 | if (!(text = (char*)malloc ((unsigned) (len + 1)))) | |
319 | { | |
320 | fclose (fp); | |
321 | return FALSE; | |
322 | } | |
323 | if (fread (text, sizeof (char), len, fp) != (size_t) len) | |
805dd538 VZ |
324 | { |
325 | } | |
a81258be RR |
326 | fclose (fp); |
327 | ||
328 | text[len] = 0; | |
8bbe427f | 329 | |
a81258be RR |
330 | if (m_windowStyle & wxTE_MULTILINE) |
331 | { | |
435fe83e RR |
332 | gint pos = 0; |
333 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos ); | |
a81258be RR |
334 | } |
335 | else | |
336 | { | |
337 | gtk_entry_set_text( GTK_ENTRY(m_text), text ); | |
338 | } | |
8bbe427f | 339 | |
a81258be RR |
340 | free (text); |
341 | m_modified = FALSE; | |
342 | return TRUE; | |
343 | } | |
112892b9 | 344 | return FALSE; |
6de97a3b | 345 | } |
c801d85f | 346 | |
a81258be | 347 | bool wxTextCtrl::SaveFile( const wxString &file ) |
c801d85f | 348 | { |
a81258be | 349 | wxCHECK_MSG( m_text != NULL, FALSE, "invalid text ctrl" ); |
8bbe427f | 350 | |
a81258be | 351 | if (file == "") return FALSE; |
8bbe427f | 352 | |
a81258be | 353 | FILE *fp; |
2ad3a34e | 354 | |
a81258be RR |
355 | if (!(fp = fopen ((char*) (const char*) file, "w"))) |
356 | { | |
357 | return FALSE; | |
358 | } | |
359 | else | |
360 | { | |
bbe0af5b | 361 | char *text = (char*) NULL; |
a81258be | 362 | gint len = 0; |
8bbe427f | 363 | |
a81258be RR |
364 | if (m_windowStyle & wxTE_MULTILINE) |
365 | { | |
366 | len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
367 | text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
368 | } | |
369 | else | |
370 | { | |
371 | text = gtk_entry_get_text( GTK_ENTRY(m_text) ); | |
372 | } | |
8bbe427f | 373 | |
a81258be | 374 | if (fwrite (text, sizeof (char), len, fp) != (size_t) len) |
96385642 VZ |
375 | { |
376 | // Did not write whole file | |
377 | } | |
8bbe427f | 378 | |
a81258be RR |
379 | // Make sure newline terminates the file |
380 | if (text[len - 1] != '\n') | |
96385642 | 381 | fputc ('\n', fp); |
a81258be RR |
382 | |
383 | fclose (fp); | |
8bbe427f | 384 | |
a81258be | 385 | if (m_windowStyle & wxTE_MULTILINE) g_free( text ); |
8bbe427f | 386 | |
a81258be RR |
387 | m_modified = FALSE; |
388 | return TRUE; | |
389 | } | |
390 | ||
391 | return TRUE; | |
6de97a3b | 392 | } |
c801d85f | 393 | |
debe6624 | 394 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
c801d85f | 395 | { |
a81258be RR |
396 | if (m_windowStyle & wxTE_MULTILINE) |
397 | { | |
398 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
399 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
400 | ||
401 | if (text) | |
402 | { | |
403 | wxString buf(""); | |
404 | long i; | |
405 | int currentLine = 0; | |
406 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
407 | if (text[i] == '\n') | |
408 | currentLine++; | |
409 | // Now get the text | |
410 | int j; | |
411 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
412 | buf += text[i]; | |
8bbe427f | 413 | |
a81258be RR |
414 | g_free( text ); |
415 | return buf; | |
416 | } | |
417 | else | |
418 | return wxEmptyString; | |
419 | } | |
420 | else | |
421 | { | |
422 | if (lineNo == 0) return GetValue(); | |
423 | return wxEmptyString; | |
424 | } | |
6de97a3b | 425 | } |
c801d85f | 426 | |
a81258be RR |
427 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
428 | { | |
2830bf19 | 429 | wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" ); |
a81258be | 430 | } |
112892b9 | 431 | |
e3ca08dd | 432 | long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 433 | { |
96385642 | 434 | if ( m_windowStyle & wxTE_MULTILINE ) |
805dd538 | 435 | { |
96385642 VZ |
436 | wxString text = GetValue(); |
437 | ||
6085b116 | 438 | // cast to prevent warning. But pos really should've been unsigned. |
2829d9e3 | 439 | if( (unsigned long)pos > text.Len() ) |
96385642 VZ |
440 | return FALSE; |
441 | ||
442 | *x=1; // Col 1 | |
443 | *y=1; // Line 1 | |
2829d9e3 VZ |
444 | |
445 | if (pos == 0) | |
446 | return TRUE; | |
447 | ||
6085b116 VZ |
448 | const char* stop = text.c_str() + pos; |
449 | for ( const char *p = text.c_str(); p < stop; p++ ) | |
96385642 VZ |
450 | { |
451 | if (*p == '\n') | |
452 | { | |
453 | (*y)++; | |
454 | *x=1; | |
455 | } | |
456 | else | |
457 | (*x)++; | |
458 | } | |
805dd538 | 459 | } |
96385642 VZ |
460 | else // single line control |
461 | { | |
2829d9e3 | 462 | if ( pos <= GTK_ENTRY(m_text)->text_length ) |
96385642 VZ |
463 | { |
464 | *y = 1; | |
465 | *x = pos; | |
466 | } | |
467 | else | |
468 | { | |
469 | // index out of bounds | |
470 | return FALSE; | |
471 | } | |
8bbe427f | 472 | } |
96385642 VZ |
473 | |
474 | return TRUE; | |
6de97a3b | 475 | } |
c801d85f | 476 | |
e3ca08dd | 477 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 478 | { |
2830bf19 | 479 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; |
805dd538 | 480 | |
2830bf19 | 481 | long pos=0; |
2829d9e3 VZ |
482 | /* This is a kludge; our XY values are 1-based, but GetLineLength() |
483 | * and --Text() start counting at 0. (and so say the docs) */ | |
484 | for( int i=1; i<y; i++ ) pos += GetLineLength(i-1) + 1; // one for '\n' | |
8bbe427f | 485 | |
2829d9e3 | 486 | pos += x-1; // Pos start with 0 |
2830bf19 | 487 | return pos; |
6de97a3b | 488 | } |
c801d85f | 489 | |
a81258be | 490 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 491 | { |
a81258be RR |
492 | wxString str = GetLineText (lineNo); |
493 | return (int) str.Length(); | |
6de97a3b | 494 | } |
c801d85f | 495 | |
a81258be | 496 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 497 | { |
2830bf19 | 498 | if (m_windowStyle & wxTE_MULTILINE) |
a81258be | 499 | { |
2830bf19 RR |
500 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
501 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
502 | ||
503 | if (text) | |
504 | { | |
505 | int currentLine = 0; | |
506 | for (int i = 0; i < len; i++ ) | |
96385642 | 507 | { |
2830bf19 | 508 | if (text[i] == '\n') |
96385642 VZ |
509 | currentLine++; |
510 | } | |
2830bf19 | 511 | g_free( text ); |
2829d9e3 VZ |
512 | |
513 | // currentLine is 0 based, add 1 to get number of lines | |
514 | return currentLine + 1; | |
2830bf19 RR |
515 | } |
516 | else | |
96385642 | 517 | { |
2830bf19 | 518 | return 0; |
96385642 | 519 | } |
a81258be RR |
520 | } |
521 | else | |
2830bf19 | 522 | { |
96385642 | 523 | return 1; |
2830bf19 | 524 | } |
6de97a3b | 525 | } |
c801d85f | 526 | |
debe6624 | 527 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 528 | { |
2830bf19 | 529 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 530 | |
2830bf19 | 531 | if (m_windowStyle & wxTE_MULTILINE) |
d59051dd | 532 | gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); |
2830bf19 | 533 | else |
d59051dd | 534 | gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos ); |
6de97a3b | 535 | } |
c801d85f | 536 | |
03f38c58 | 537 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 538 | { |
2830bf19 | 539 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 540 | |
d59051dd VZ |
541 | if (m_windowStyle & wxTE_MULTILINE) |
542 | SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text))); | |
543 | else | |
544 | gtk_entry_set_position( GTK_ENTRY(m_text), -1 ); | |
6de97a3b | 545 | } |
c801d85f | 546 | |
debe6624 | 547 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 548 | { |
2830bf19 | 549 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 550 | |
2830bf19 RR |
551 | if (m_windowStyle & wxTE_MULTILINE) |
552 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); | |
553 | else | |
554 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); | |
6de97a3b | 555 | } |
c801d85f | 556 | |
debe6624 | 557 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 558 | { |
2830bf19 | 559 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 560 | |
2830bf19 | 561 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 562 | } |
c801d85f | 563 | |
debe6624 | 564 | void wxTextCtrl::ShowPosition( long WXUNUSED(pos) ) |
c801d85f | 565 | { |
2830bf19 | 566 | wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" ); |
6de97a3b | 567 | } |
c801d85f | 568 | |
03f38c58 | 569 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 570 | { |
2830bf19 | 571 | wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" ); |
8bbe427f | 572 | |
2830bf19 | 573 | return (long) GTK_EDITABLE(m_text)->current_pos; |
6de97a3b | 574 | } |
c801d85f | 575 | |
03f38c58 | 576 | long wxTextCtrl::GetLastPosition() const |
c801d85f | 577 | { |
2830bf19 | 578 | wxCHECK_MSG( m_text != NULL, 0, "invalid text ctrl" ); |
8bbe427f | 579 | |
2830bf19 RR |
580 | int pos = 0; |
581 | if (m_windowStyle & wxTE_MULTILINE) | |
582 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); | |
583 | else | |
584 | pos = GTK_ENTRY(m_text)->text_length; | |
805dd538 | 585 | |
2830bf19 | 586 | return (long)pos-1; |
6de97a3b | 587 | } |
c801d85f | 588 | |
debe6624 | 589 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 590 | { |
2830bf19 | 591 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 592 | |
2830bf19 | 593 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 594 | } |
c801d85f | 595 | |
debe6624 | 596 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 597 | { |
2830bf19 | 598 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 599 | |
2830bf19 RR |
600 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
601 | if (value.IsNull()) return; | |
435fe83e | 602 | gint pos = (gint)from; |
2830bf19 | 603 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
6de97a3b | 604 | } |
c801d85f | 605 | |
03f38c58 | 606 | void wxTextCtrl::Cut() |
c801d85f | 607 | { |
2830bf19 | 608 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 609 | |
75ed1d15 | 610 | #if (GTK_MINOR_VERSION == 1) |
2830bf19 | 611 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) ); |
75ed1d15 | 612 | #else |
2830bf19 | 613 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 614 | #endif |
6de97a3b | 615 | } |
c801d85f | 616 | |
03f38c58 | 617 | void wxTextCtrl::Copy() |
c801d85f | 618 | { |
2830bf19 | 619 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 620 | |
75ed1d15 | 621 | #if (GTK_MINOR_VERSION == 1) |
2830bf19 | 622 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) ); |
75ed1d15 | 623 | #else |
2830bf19 | 624 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 625 | #endif |
6de97a3b | 626 | } |
c801d85f | 627 | |
03f38c58 | 628 | void wxTextCtrl::Paste() |
c801d85f | 629 | { |
2830bf19 | 630 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 631 | |
75ed1d15 | 632 | #if (GTK_MINOR_VERSION == 1) |
2830bf19 | 633 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) ); |
75ed1d15 | 634 | #else |
2830bf19 | 635 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 636 | #endif |
6de97a3b | 637 | } |
c801d85f | 638 | |
03f38c58 | 639 | void wxTextCtrl::Clear() |
c801d85f | 640 | { |
2830bf19 | 641 | SetValue( "" ); |
6de97a3b | 642 | } |
c801d85f | 643 | |
903f689b | 644 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 645 | { |
2830bf19 | 646 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
805dd538 | 647 | |
2830bf19 RR |
648 | if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
649 | { | |
650 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
651 | event.SetEventObject(this); | |
652 | if (GetEventHandler()->ProcessEvent(event)) return; | |
653 | } | |
903f689b | 654 | |
2830bf19 | 655 | key_event.Skip(); |
6de97a3b | 656 | } |
c801d85f | 657 | |
f5abe911 | 658 | #ifndef NO_TEXT_WINDOW_STREAM |
46dc76ba | 659 | int wxTextCtrl::overflow( int WXUNUSED(c) ) |
c801d85f | 660 | { |
2830bf19 RR |
661 | int len = pptr() - pbase(); |
662 | char *txt = new char[len+1]; | |
663 | strncpy(txt, pbase(), len); | |
664 | txt[len] = '\0'; | |
665 | (*this) << txt; | |
666 | setp(pbase(), epptr()); | |
667 | delete[] txt; | |
668 | return EOF; | |
6de97a3b | 669 | } |
c801d85f | 670 | |
03f38c58 | 671 | int wxTextCtrl::sync() |
c801d85f | 672 | { |
2830bf19 RR |
673 | int len = pptr() - pbase(); |
674 | char *txt = new char[len+1]; | |
675 | strncpy(txt, pbase(), len); | |
676 | txt[len] = '\0'; | |
677 | (*this) << txt; | |
678 | setp(pbase(), epptr()); | |
679 | delete[] txt; | |
680 | return 0; | |
6de97a3b | 681 | } |
c801d85f | 682 | |
03f38c58 | 683 | int wxTextCtrl::underflow() |
c801d85f | 684 | { |
2830bf19 | 685 | return EOF; |
6de97a3b | 686 | } |
c801d85f KB |
687 | |
688 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
689 | { | |
2830bf19 RR |
690 | WriteText(s); |
691 | return *this; | |
c801d85f KB |
692 | } |
693 | ||
debe6624 | 694 | wxTextCtrl& wxTextCtrl::operator<<(float f) |
c801d85f | 695 | { |
2830bf19 RR |
696 | static char buf[100]; |
697 | sprintf(buf, "%.2f", f); | |
698 | WriteText(buf); | |
699 | return *this; | |
c801d85f KB |
700 | } |
701 | ||
debe6624 | 702 | wxTextCtrl& wxTextCtrl::operator<<(double d) |
c801d85f | 703 | { |
2830bf19 RR |
704 | static char buf[100]; |
705 | sprintf(buf, "%.2f", d); | |
706 | WriteText(buf); | |
707 | return *this; | |
c801d85f KB |
708 | } |
709 | ||
debe6624 | 710 | wxTextCtrl& wxTextCtrl::operator<<(int i) |
c801d85f | 711 | { |
2830bf19 RR |
712 | static char buf[100]; |
713 | sprintf(buf, "%i", i); | |
714 | WriteText(buf); | |
715 | return *this; | |
c801d85f KB |
716 | } |
717 | ||
debe6624 | 718 | wxTextCtrl& wxTextCtrl::operator<<(long i) |
c801d85f | 719 | { |
2830bf19 RR |
720 | static char buf[100]; |
721 | sprintf(buf, "%ld", i); | |
722 | WriteText(buf); | |
723 | return *this; | |
c801d85f KB |
724 | } |
725 | ||
726 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
727 | { | |
2830bf19 | 728 | char buf[2]; |
c801d85f | 729 | |
2830bf19 RR |
730 | buf[0] = c; |
731 | buf[1] = 0; | |
732 | WriteText(buf); | |
733 | return *this; | |
c801d85f | 734 | } |
f5abe911 | 735 | #endif |
c801d85f | 736 | |
03f38c58 | 737 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 738 | { |
ae0bdb01 | 739 | return GTK_WIDGET(m_text); |
6de97a3b | 740 | } |
e3e65dac | 741 | |
903f689b RR |
742 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
743 | { | |
ae0bdb01 RR |
744 | if (m_windowStyle & wxTE_MULTILINE) |
745 | return (window == GTK_TEXT(m_text)->text_area); | |
746 | else | |
747 | return (window == GTK_ENTRY(m_text)->text_area); | |
903f689b | 748 | } |
e3e65dac | 749 | |
58614078 | 750 | void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) ) |
868a2826 | 751 | { |
ae0bdb01 | 752 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 753 | |
ae0bdb01 | 754 | // doesn't work |
58614078 RR |
755 | } |
756 | ||
757 | void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) ) | |
758 | { | |
ae0bdb01 | 759 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 760 | |
ae0bdb01 | 761 | // doesn't work |
868a2826 | 762 | } |
e3e65dac | 763 | |
68dda785 VZ |
764 | void wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
765 | { | |
ae0bdb01 | 766 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
a81258be | 767 | |
ae0bdb01 | 768 | wxControl::SetBackgroundColour( colour ); |
8bbe427f | 769 | |
ae0bdb01 | 770 | wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ); |
805dd538 VZ |
771 | if (sysbg.Red() == colour.Red() && |
772 | sysbg.Green() == colour.Green() && | |
ae0bdb01 RR |
773 | sysbg.Blue() == colour.Blue()) |
774 | { | |
775 | return; | |
805dd538 VZ |
776 | } |
777 | ||
ae0bdb01 | 778 | if (!m_backgroundColour.Ok()) return; |
8bbe427f | 779 | |
ae0bdb01 RR |
780 | if (m_windowStyle & wxTE_MULTILINE) |
781 | { | |
782 | GdkWindow *window = GTK_TEXT(m_text)->text_area; | |
783 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); | |
784 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
785 | gdk_window_clear( window ); | |
786 | } | |
58614078 RR |
787 | } |
788 | ||
789 | void wxTextCtrl::ApplyWidgetStyle() | |
790 | { | |
ae0bdb01 RR |
791 | if (m_windowStyle & wxTE_MULTILINE) |
792 | { | |
2830bf19 | 793 | // how ? |
805dd538 | 794 | } |
ae0bdb01 RR |
795 | else |
796 | { | |
797 | SetWidgetStyle(); | |
798 | gtk_widget_set_style( m_text, m_widgetStyle ); | |
799 | } | |
68dda785 | 800 | } |
f96aa4d9 | 801 |