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