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