]>
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 | // idle system | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | extern void wxapp_install_idle_handler(); | |
32 | extern bool g_isIdle; | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // data | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern bool g_blockEventsOnDrag; | |
39 | ||
40 | //----------------------------------------------------------------------------- | |
41 | // "changed" | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | static void | |
45 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) | |
46 | { | |
47 | if (g_isIdle) wxapp_install_idle_handler(); | |
48 | ||
49 | if (!win->m_hasVMT) return; | |
50 | ||
51 | win->SetModified(); | |
52 | ||
53 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->m_windowId ); | |
54 | event.SetString( win->GetValue() ); | |
55 | event.SetEventObject( win ); | |
56 | win->GetEventHandler()->ProcessEvent( event ); | |
57 | } | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // "changed" from vertical scrollbar | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | static void | |
64 | gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) | |
65 | { | |
66 | if (g_isIdle) wxapp_install_idle_handler(); | |
67 | ||
68 | win->CalculateScrollbar(); | |
69 | ||
70 | if (!win->m_hasVMT) return; | |
71 | } | |
72 | ||
73 | //----------------------------------------------------------------------------- | |
74 | // wxTextCtrl | |
75 | //----------------------------------------------------------------------------- | |
76 | ||
77 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
78 | ||
79 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
80 | EVT_CHAR(wxTextCtrl::OnChar) | |
81 | ||
82 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
83 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
84 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
85 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
86 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
87 | ||
88 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
89 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
90 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
91 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
92 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
93 | END_EVENT_TABLE() | |
94 | ||
95 | #ifndef NO_TEXT_WINDOW_STREAM | |
96 | wxTextCtrl::wxTextCtrl() : streambuf() | |
97 | { | |
98 | if (allocate()) setp(base(),ebuf()); | |
99 | ||
100 | m_modified = FALSE; | |
101 | } | |
102 | #else | |
103 | wxTextCtrl::wxTextCtrl() | |
104 | { | |
105 | m_modified = FALSE; | |
106 | } | |
107 | #endif | |
108 | ||
109 | #ifndef NO_TEXT_WINDOW_STREAM | |
110 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, | |
111 | const wxPoint &pos, const wxSize &size, | |
112 | int style, const wxValidator& validator, const wxString &name ) : streambuf() | |
113 | { | |
114 | if (allocate()) setp(base(),ebuf()); | |
115 | ||
116 | m_modified = FALSE; | |
117 | Create( parent, id, value, pos, size, style, validator, name ); | |
118 | } | |
119 | #else | |
120 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, | |
121 | const wxPoint &pos, const wxSize &size, | |
122 | int style, const wxValidator& validator, const wxString &name ) | |
123 | { | |
124 | m_modified = FALSE; | |
125 | Create( parent, id, value, pos, size, style, validator, name ); | |
126 | } | |
127 | #endif | |
128 | ||
129 | bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, | |
130 | const wxPoint &pos, const wxSize &size, | |
131 | int style, const wxValidator& validator, const wxString &name ) | |
132 | { | |
133 | m_needParent = TRUE; | |
134 | m_acceptsFocus = TRUE; | |
135 | ||
136 | PreCreation( parent, id, pos, size, style, name ); | |
137 | ||
138 | SetValidator( validator ); | |
139 | ||
140 | m_vScrollbarVisible = FALSE; | |
141 | ||
142 | bool multi_line = (style & wxTE_MULTILINE) != 0; | |
143 | if (multi_line) | |
144 | { | |
145 | /* a multi-line edit control: create a vertical scrollbar by default and | |
146 | horizontal if requested */ | |
147 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; | |
148 | ||
149 | /* create our control ... */ | |
150 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); | |
151 | ||
152 | /* ... and put into the upper left hand corner of the table */ | |
153 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); | |
154 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
155 | ||
156 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, | |
157 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), | |
158 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), | |
159 | 0, 0); | |
160 | ||
161 | /* always wrap words */ | |
162 | gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE ); | |
163 | ||
164 | /* put the horizontal scrollbar in the lower left hand corner */ | |
165 | if (bHasHScrollbar) | |
166 | { | |
167 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); | |
168 | GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS ); | |
169 | ||
170 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, | |
171 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), | |
172 | GTK_FILL, | |
173 | 0, 0); | |
174 | gtk_widget_show(hscrollbar); | |
175 | ||
176 | #if (GTK_MINOR_VERSION > 0) | |
177 | /* don't wrap lines, otherwise we wouldn't need the scrollbar */ | |
178 | gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE ); | |
179 | #endif | |
180 | } | |
181 | ||
182 | /* finally, put the vertical scrollbar in the upper right corner */ | |
183 | m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj ); | |
184 | GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS ); | |
185 | ||
186 | gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1, | |
187 | GTK_FILL, | |
188 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), | |
189 | 0, 0); | |
190 | } | |
191 | else | |
192 | { | |
193 | /* a single-line text control: no need for scrollbars */ | |
194 | m_widget = | |
195 | m_text = gtk_entry_new(); | |
196 | } | |
197 | ||
198 | wxSize newSize = size; | |
199 | if (newSize.x == -1) newSize.x = 80; | |
200 | if (newSize.y == -1) newSize.y = 26; | |
201 | SetSize( newSize.x, newSize.y ); | |
202 | ||
203 | m_parent->AddChild( this ); | |
204 | ||
205 | (m_parent->m_insertCallback)( m_parent, this ); | |
206 | ||
207 | PostCreation(); | |
208 | ||
209 | if (multi_line) | |
210 | gtk_widget_show(m_text); | |
211 | ||
212 | /* we want to be notified about text changes */ | |
213 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", | |
214 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
215 | ||
216 | if (multi_line) | |
217 | { | |
218 | gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed", | |
219 | (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this ); | |
220 | } | |
221 | ||
222 | if (!value.IsEmpty()) | |
223 | { | |
224 | gint tmp = 0; | |
225 | #if wxUSE_UNICODE | |
226 | wxWX2MBbuf val = value.mbc_str(); | |
227 | gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp ); | |
228 | #else | |
229 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); | |
230 | #endif | |
231 | ||
232 | if (multi_line) | |
233 | { | |
234 | /* bring editable's cursor uptodate. bug in GTK. */ | |
235 | ||
236 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
237 | } | |
238 | } | |
239 | ||
240 | if (style & wxTE_PASSWORD) | |
241 | { | |
242 | if (!multi_line) | |
243 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
244 | } | |
245 | ||
246 | if (style & wxTE_READONLY) | |
247 | { | |
248 | if (!multi_line) | |
249 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
250 | } | |
251 | else | |
252 | { | |
253 | if (multi_line) | |
254 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
255 | } | |
256 | ||
257 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
258 | SetForegroundColour( parent->GetForegroundColour() ); | |
259 | ||
260 | Show( TRUE ); | |
261 | ||
262 | return TRUE; | |
263 | } | |
264 | ||
265 | void wxTextCtrl::CalculateScrollbar() | |
266 | { | |
267 | if ((m_windowStyle & wxTE_MULTILINE) == 0) return; | |
268 | ||
269 | GtkAdjustment *adj = GTK_TEXT(m_text)->vadj; | |
270 | ||
271 | if (adj->upper - adj->page_size < 0.8) | |
272 | { | |
273 | if (m_vScrollbarVisible) | |
274 | { | |
275 | gtk_widget_hide( m_vScrollbar ); | |
276 | ||
277 | m_vScrollbarVisible = FALSE; | |
278 | } | |
279 | } | |
280 | else | |
281 | { | |
282 | if (!m_vScrollbarVisible) | |
283 | { | |
284 | gtk_widget_show( m_vScrollbar ); | |
285 | ||
286 | m_vScrollbarVisible = TRUE; | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | wxString wxTextCtrl::GetValue() const | |
292 | { | |
293 | wxCHECK_MSG( m_text != NULL, _T(""), _T("invalid text ctrl") ); | |
294 | ||
295 | wxString tmp; | |
296 | if (m_windowStyle & wxTE_MULTILINE) | |
297 | { | |
298 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
299 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
300 | tmp = wxString(text,*wxConv_current); | |
301 | g_free( text ); | |
302 | } | |
303 | else | |
304 | { | |
305 | tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConv_current); | |
306 | } | |
307 | return tmp; | |
308 | } | |
309 | ||
310 | void wxTextCtrl::SetValue( const wxString &value ) | |
311 | { | |
312 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
313 | ||
314 | wxString tmp = _T(""); | |
315 | if (!value.IsNull()) tmp = value; | |
316 | if (m_windowStyle & wxTE_MULTILINE) | |
317 | { | |
318 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
319 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
320 | len = 0; | |
321 | #if wxUSE_UNICODE | |
322 | wxWX2MBbuf tmpbuf = tmp.mbc_str(); | |
323 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len ); | |
324 | #else | |
325 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp.mbc_str(), tmp.Length(), &len ); | |
326 | #endif | |
327 | } | |
328 | else | |
329 | { | |
330 | gtk_entry_set_text( GTK_ENTRY(m_text), tmp.mbc_str() ); | |
331 | } | |
332 | } | |
333 | ||
334 | void wxTextCtrl::WriteText( const wxString &text ) | |
335 | { | |
336 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
337 | ||
338 | if (text.IsNull()) return; | |
339 | ||
340 | if (m_windowStyle & wxTE_MULTILINE) | |
341 | { | |
342 | /* this moves the cursor pos to behind the inserted text */ | |
343 | gint len = GTK_EDITABLE(m_text)->current_pos; | |
344 | ||
345 | #if wxUSE_UNICODE | |
346 | wxWX2MBbuf buf = text.mbc_str(); | |
347 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len ); | |
348 | #else | |
349 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); | |
350 | #endif | |
351 | ||
352 | /* bring editable's cursor uptodate. bug in GTK. */ | |
353 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
354 | } | |
355 | else | |
356 | { | |
357 | /* this moves the cursor pos to behind the inserted text */ | |
358 | gint len = GTK_EDITABLE(m_text)->current_pos; | |
359 | #if wxUSE_UNICODE | |
360 | wxWX2MBbuf buf = text.mbc_str(); | |
361 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len ); | |
362 | #else | |
363 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); | |
364 | #endif | |
365 | ||
366 | /* bring editable's cursor uptodate. bug in GTK. */ | |
367 | GTK_EDITABLE(m_text)->current_pos += text.Len(); | |
368 | ||
369 | /* bring entry's cursor uptodate. bug in GTK. */ | |
370 | gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos ); | |
371 | } | |
372 | } | |
373 | ||
374 | void wxTextCtrl::AppendText( const wxString &text ) | |
375 | { | |
376 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
377 | ||
378 | if (m_windowStyle & wxTE_MULTILINE) | |
379 | { | |
380 | /* we'll insert at the last position */ | |
381 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
382 | #if wxUSE_UNICODE | |
383 | wxWX2MBbuf buf = text.mbc_str(); | |
384 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len ); | |
385 | #else | |
386 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); | |
387 | #endif | |
388 | ||
389 | /* bring editable's cursor uptodate. bug in GTK. */ | |
390 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
391 | } | |
392 | else | |
393 | { | |
394 | gtk_entry_append_text( GTK_ENTRY(m_text), text.mbc_str() ); | |
395 | } | |
396 | } | |
397 | ||
398 | bool wxTextCtrl::LoadFile( const wxString &file ) | |
399 | { | |
400 | wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") ); | |
401 | ||
402 | if (!wxFileExists(file)) return FALSE; | |
403 | ||
404 | Clear(); | |
405 | ||
406 | FILE *fp = (FILE*) NULL; | |
407 | struct stat statb; | |
408 | ||
409 | if ((stat (FNSTRINGCAST file.fn_str(), &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || | |
410 | !(fp = fopen (FNSTRINGCAST file.fn_str(), "r"))) | |
411 | { | |
412 | return FALSE; | |
413 | } | |
414 | else | |
415 | { | |
416 | gint len = statb.st_size; | |
417 | char *text; | |
418 | if (!(text = (char*)malloc ((unsigned) (len + 1)))) | |
419 | { | |
420 | fclose (fp); | |
421 | return FALSE; | |
422 | } | |
423 | if (fread (text, sizeof (char), len, fp) != (size_t) len) | |
424 | { | |
425 | } | |
426 | fclose (fp); | |
427 | ||
428 | text[len] = 0; | |
429 | ||
430 | if (m_windowStyle & wxTE_MULTILINE) | |
431 | { | |
432 | gint pos = 0; | |
433 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, len, &pos ); | |
434 | } | |
435 | else | |
436 | { | |
437 | gtk_entry_set_text( GTK_ENTRY(m_text), text ); | |
438 | } | |
439 | ||
440 | free (text); | |
441 | m_modified = FALSE; | |
442 | return TRUE; | |
443 | } | |
444 | return FALSE; | |
445 | } | |
446 | ||
447 | bool wxTextCtrl::SaveFile( const wxString &file ) | |
448 | { | |
449 | wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") ); | |
450 | ||
451 | if (file == _T("")) return FALSE; | |
452 | ||
453 | FILE *fp; | |
454 | ||
455 | if (!(fp = fopen (FNSTRINGCAST file.fn_str(), "w"))) | |
456 | { | |
457 | return FALSE; | |
458 | } | |
459 | else | |
460 | { | |
461 | char *text = (char*) NULL; | |
462 | gint len = 0; | |
463 | ||
464 | if (m_windowStyle & wxTE_MULTILINE) | |
465 | { | |
466 | len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
467 | text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
468 | } | |
469 | else | |
470 | { | |
471 | text = gtk_entry_get_text( GTK_ENTRY(m_text) ); | |
472 | } | |
473 | ||
474 | if (fwrite (text, sizeof (char), len, fp) != (size_t) len) | |
475 | { | |
476 | // Did not write whole file | |
477 | } | |
478 | ||
479 | // Make sure newline terminates the file | |
480 | if (text[len - 1] != '\n') | |
481 | fputc ('\n', fp); | |
482 | ||
483 | fclose (fp); | |
484 | ||
485 | if (m_windowStyle & wxTE_MULTILINE) g_free( text ); | |
486 | ||
487 | m_modified = FALSE; | |
488 | return TRUE; | |
489 | } | |
490 | ||
491 | return TRUE; | |
492 | } | |
493 | ||
494 | wxString wxTextCtrl::GetLineText( long lineNo ) const | |
495 | { | |
496 | if (m_windowStyle & wxTE_MULTILINE) | |
497 | { | |
498 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
499 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
500 | ||
501 | if (text) | |
502 | { | |
503 | wxString buf(_T("")); | |
504 | long i; | |
505 | int currentLine = 0; | |
506 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
507 | if (text[i] == '\n') | |
508 | currentLine++; | |
509 | // Now get the text | |
510 | int j; | |
511 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
512 | buf += text[i]; | |
513 | ||
514 | g_free( text ); | |
515 | return buf; | |
516 | } | |
517 | else | |
518 | return wxEmptyString; | |
519 | } | |
520 | else | |
521 | { | |
522 | if (lineNo == 0) return GetValue(); | |
523 | return wxEmptyString; | |
524 | } | |
525 | } | |
526 | ||
527 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) | |
528 | { | |
529 | /* If you implement this, don't forget to update the documentation! | |
530 | * (file docs/latex/wx/text.tex) */ | |
531 | wxFAIL_MSG( _T("wxTextCtrl::OnDropFiles not implemented") ); | |
532 | } | |
533 | ||
534 | long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const | |
535 | { | |
536 | if ( m_windowStyle & wxTE_MULTILINE ) | |
537 | { | |
538 | wxString text = GetValue(); | |
539 | ||
540 | // cast to prevent warning. But pos really should've been unsigned. | |
541 | if( (unsigned long)pos > text.Len() ) | |
542 | return FALSE; | |
543 | ||
544 | *x=0; // First Col | |
545 | *y=0; // First Line | |
546 | ||
547 | const wxChar* stop = text.c_str() + pos; | |
548 | for ( const wxChar *p = text.c_str(); p < stop; p++ ) | |
549 | { | |
550 | if (*p == _T('\n')) | |
551 | { | |
552 | (*y)++; | |
553 | *x=0; | |
554 | } | |
555 | else | |
556 | (*x)++; | |
557 | } | |
558 | } | |
559 | else // single line control | |
560 | { | |
561 | if ( pos <= GTK_ENTRY(m_text)->text_length ) | |
562 | { | |
563 | *y = 0; | |
564 | *x = pos; | |
565 | } | |
566 | else | |
567 | { | |
568 | // index out of bounds | |
569 | return FALSE; | |
570 | } | |
571 | } | |
572 | ||
573 | return TRUE; | |
574 | } | |
575 | ||
576 | long wxTextCtrl::XYToPosition(long x, long y ) const | |
577 | { | |
578 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; | |
579 | ||
580 | long pos=0; | |
581 | for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n' | |
582 | ||
583 | pos += x; | |
584 | return pos; | |
585 | } | |
586 | ||
587 | int wxTextCtrl::GetLineLength(long lineNo) const | |
588 | { | |
589 | wxString str = GetLineText (lineNo); | |
590 | return (int) str.Length(); | |
591 | } | |
592 | ||
593 | int wxTextCtrl::GetNumberOfLines() const | |
594 | { | |
595 | if (m_windowStyle & wxTE_MULTILINE) | |
596 | { | |
597 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
598 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
599 | ||
600 | if (text) | |
601 | { | |
602 | int currentLine = 0; | |
603 | for (int i = 0; i < len; i++ ) | |
604 | { | |
605 | if (text[i] == '\n') | |
606 | currentLine++; | |
607 | } | |
608 | g_free( text ); | |
609 | ||
610 | // currentLine is 0 based, add 1 to get number of lines | |
611 | return currentLine + 1; | |
612 | } | |
613 | else | |
614 | { | |
615 | return 0; | |
616 | } | |
617 | } | |
618 | else | |
619 | { | |
620 | return 1; | |
621 | } | |
622 | } | |
623 | ||
624 | void wxTextCtrl::SetInsertionPoint( long pos ) | |
625 | { | |
626 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
627 | ||
628 | if (m_windowStyle & wxTE_MULTILINE) | |
629 | { | |
630 | /* seems to be broken in GTK 1.0.X: | |
631 | gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */ | |
632 | ||
633 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_text), | |
634 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
635 | ||
636 | /* we fake a set_point by inserting and deleting. as the user | |
637 | isn't supposed to get to know about thos non-sense, we | |
638 | disconnect so that no events are sent to the user program. */ | |
639 | ||
640 | gint tmp = (gint)pos; | |
641 | gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp ); | |
642 | gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp ); | |
643 | ||
644 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", | |
645 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
646 | ||
647 | /* bring editable's cursor uptodate. another bug in GTK. */ | |
648 | ||
649 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
650 | } | |
651 | else | |
652 | { | |
653 | gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos ); | |
654 | ||
655 | /* bring editable's cursor uptodate. bug in GTK. */ | |
656 | ||
657 | GTK_EDITABLE(m_text)->current_pos = pos; | |
658 | } | |
659 | } | |
660 | ||
661 | void wxTextCtrl::SetInsertionPointEnd() | |
662 | { | |
663 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
664 | ||
665 | if (m_windowStyle & wxTE_MULTILINE) | |
666 | SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text))); | |
667 | else | |
668 | gtk_entry_set_position( GTK_ENTRY(m_text), -1 ); | |
669 | } | |
670 | ||
671 | void wxTextCtrl::SetEditable( bool editable ) | |
672 | { | |
673 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
674 | ||
675 | if (m_windowStyle & wxTE_MULTILINE) | |
676 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); | |
677 | else | |
678 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); | |
679 | } | |
680 | ||
681 | void wxTextCtrl::SetSelection( long from, long to ) | |
682 | { | |
683 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
684 | ||
685 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
686 | } | |
687 | ||
688 | void wxTextCtrl::ShowPosition( long WXUNUSED(pos) ) | |
689 | { | |
690 | wxFAIL_MSG( _T("wxTextCtrl::ShowPosition not implemented") ); | |
691 | } | |
692 | ||
693 | long wxTextCtrl::GetInsertionPoint() const | |
694 | { | |
695 | wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") ); | |
696 | ||
697 | return (long) GTK_EDITABLE(m_text)->current_pos; | |
698 | } | |
699 | ||
700 | long wxTextCtrl::GetLastPosition() const | |
701 | { | |
702 | wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") ); | |
703 | ||
704 | int pos = 0; | |
705 | if (m_windowStyle & wxTE_MULTILINE) | |
706 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); | |
707 | else | |
708 | pos = GTK_ENTRY(m_text)->text_length; | |
709 | ||
710 | return (long)pos; | |
711 | } | |
712 | ||
713 | void wxTextCtrl::Remove( long from, long to ) | |
714 | { | |
715 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
716 | ||
717 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
718 | } | |
719 | ||
720 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) | |
721 | { | |
722 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
723 | ||
724 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
725 | if (value.IsNull()) return; | |
726 | gint pos = (gint)from; | |
727 | #if wxUSE_UNICODE | |
728 | wxWX2MBbuf buf = value.mbc_str(); | |
729 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos ); | |
730 | #else | |
731 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); | |
732 | #endif | |
733 | } | |
734 | ||
735 | void wxTextCtrl::Cut() | |
736 | { | |
737 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
738 | ||
739 | #if (GTK_MINOR_VERSION > 0) | |
740 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) ); | |
741 | #else | |
742 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 ); | |
743 | #endif | |
744 | } | |
745 | ||
746 | void wxTextCtrl::Copy() | |
747 | { | |
748 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
749 | ||
750 | #if (GTK_MINOR_VERSION > 0) | |
751 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) ); | |
752 | #else | |
753 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 ); | |
754 | #endif | |
755 | } | |
756 | ||
757 | void wxTextCtrl::Paste() | |
758 | { | |
759 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
760 | ||
761 | #if (GTK_MINOR_VERSION > 0) | |
762 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) ); | |
763 | #else | |
764 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 ); | |
765 | #endif | |
766 | } | |
767 | ||
768 | bool wxTextCtrl::CanCopy() const | |
769 | { | |
770 | // Can copy if there's a selection | |
771 | long from, to; | |
772 | GetSelection(& from, & to); | |
773 | return (from != to) ; | |
774 | } | |
775 | ||
776 | bool wxTextCtrl::CanCut() const | |
777 | { | |
778 | // Can cut if there's a selection | |
779 | long from, to; | |
780 | GetSelection(& from, & to); | |
781 | return (from != to) ; | |
782 | } | |
783 | ||
784 | bool wxTextCtrl::CanPaste() const | |
785 | { | |
786 | return IsEditable() ; | |
787 | } | |
788 | ||
789 | // Undo/redo | |
790 | void wxTextCtrl::Undo() | |
791 | { | |
792 | // TODO | |
793 | wxFAIL_MSG( _T("wxTextCtrl::Undo not implemented") ); | |
794 | } | |
795 | ||
796 | void wxTextCtrl::Redo() | |
797 | { | |
798 | // TODO | |
799 | wxFAIL_MSG( _T("wxTextCtrl::Redo not implemented") ); | |
800 | } | |
801 | ||
802 | bool wxTextCtrl::CanUndo() const | |
803 | { | |
804 | // TODO | |
805 | wxFAIL_MSG( _T("wxTextCtrl::CanUndo not implemented") ); | |
806 | return FALSE; | |
807 | } | |
808 | ||
809 | bool wxTextCtrl::CanRedo() const | |
810 | { | |
811 | // TODO | |
812 | wxFAIL_MSG( _T("wxTextCtrl::CanRedo not implemented") ); | |
813 | return FALSE; | |
814 | } | |
815 | ||
816 | // If the return values from and to are the same, there is no | |
817 | // selection. | |
818 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
819 | { | |
820 | // TODO | |
821 | *from = 0; | |
822 | *to = 0; | |
823 | wxFAIL_MSG( _T("wxTextCtrl::GetSelection not implemented") ); | |
824 | } | |
825 | ||
826 | bool wxTextCtrl::IsEditable() const | |
827 | { | |
828 | // TODO | |
829 | wxFAIL_MSG( _T("wxTextCtrl::IsEditable not implemented") ); | |
830 | return FALSE; | |
831 | } | |
832 | ||
833 | void wxTextCtrl::Clear() | |
834 | { | |
835 | SetValue( _T("") ); | |
836 | } | |
837 | ||
838 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) | |
839 | { | |
840 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
841 | ||
842 | if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) | |
843 | { | |
844 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
845 | event.SetEventObject(this); | |
846 | if (GetEventHandler()->ProcessEvent(event)) return; | |
847 | } | |
848 | ||
849 | key_event.Skip(); | |
850 | } | |
851 | ||
852 | #ifndef NO_TEXT_WINDOW_STREAM | |
853 | int wxTextCtrl::overflow( int WXUNUSED(c) ) | |
854 | { | |
855 | int len = pptr() - pbase(); | |
856 | char *txt = new char[len+1]; | |
857 | strncpy(txt, pbase(), len); | |
858 | txt[len] = '\0'; | |
859 | (*this) << txt; | |
860 | setp(pbase(), epptr()); | |
861 | delete[] txt; | |
862 | return EOF; | |
863 | } | |
864 | ||
865 | int wxTextCtrl::sync() | |
866 | { | |
867 | int len = pptr() - pbase(); | |
868 | char *txt = new char[len+1]; | |
869 | strncpy(txt, pbase(), len); | |
870 | txt[len] = '\0'; | |
871 | (*this) << txt; | |
872 | setp(pbase(), epptr()); | |
873 | delete[] txt; | |
874 | return 0; | |
875 | } | |
876 | ||
877 | int wxTextCtrl::underflow() | |
878 | { | |
879 | return EOF; | |
880 | } | |
881 | ||
882 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
883 | { | |
884 | AppendText(s); | |
885 | return *this; | |
886 | } | |
887 | ||
888 | wxTextCtrl& wxTextCtrl::operator<<(float f) | |
889 | { | |
890 | static char buf[100]; | |
891 | sprintf(buf, "%.2f", f); | |
892 | AppendText(buf); | |
893 | return *this; | |
894 | } | |
895 | ||
896 | wxTextCtrl& wxTextCtrl::operator<<(double d) | |
897 | { | |
898 | static char buf[100]; | |
899 | sprintf(buf, "%.2f", d); | |
900 | AppendText(buf); | |
901 | return *this; | |
902 | } | |
903 | ||
904 | wxTextCtrl& wxTextCtrl::operator<<(int i) | |
905 | { | |
906 | static char buf[100]; | |
907 | sprintf(buf, "%i", i); | |
908 | AppendText(buf); | |
909 | return *this; | |
910 | } | |
911 | ||
912 | wxTextCtrl& wxTextCtrl::operator<<(long i) | |
913 | { | |
914 | static char buf[100]; | |
915 | sprintf(buf, "%ld", i); | |
916 | AppendText(buf); | |
917 | return *this; | |
918 | } | |
919 | ||
920 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
921 | { | |
922 | char buf[2]; | |
923 | ||
924 | buf[0] = c; | |
925 | buf[1] = 0; | |
926 | AppendText(buf); | |
927 | return *this; | |
928 | } | |
929 | #endif | |
930 | ||
931 | GtkWidget* wxTextCtrl::GetConnectWidget() | |
932 | { | |
933 | return GTK_WIDGET(m_text); | |
934 | } | |
935 | ||
936 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) | |
937 | { | |
938 | if (m_windowStyle & wxTE_MULTILINE) | |
939 | return (window == GTK_TEXT(m_text)->text_area); | |
940 | else | |
941 | return (window == GTK_ENTRY(m_text)->text_area); | |
942 | } | |
943 | ||
944 | void wxTextCtrl::SetFont( const wxFont &WXUNUSED(font) ) | |
945 | { | |
946 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
947 | ||
948 | // doesn't work | |
949 | } | |
950 | ||
951 | void wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) ) | |
952 | { | |
953 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
954 | ||
955 | // doesn't work | |
956 | } | |
957 | ||
958 | void wxTextCtrl::SetBackgroundColour( const wxColour &colour ) | |
959 | { | |
960 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); | |
961 | ||
962 | wxControl::SetBackgroundColour( colour ); | |
963 | ||
964 | if (!m_widget->window) return; | |
965 | ||
966 | wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ); | |
967 | if (sysbg.Red() == colour.Red() && | |
968 | sysbg.Green() == colour.Green() && | |
969 | sysbg.Blue() == colour.Blue()) | |
970 | { | |
971 | return; | |
972 | } | |
973 | ||
974 | if (!m_backgroundColour.Ok()) return; | |
975 | ||
976 | if (m_windowStyle & wxTE_MULTILINE) | |
977 | { | |
978 | GdkWindow *window = GTK_TEXT(m_text)->text_area; | |
979 | if (!window) return; | |
980 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); | |
981 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
982 | gdk_window_clear( window ); | |
983 | } | |
984 | } | |
985 | ||
986 | void wxTextCtrl::ApplyWidgetStyle() | |
987 | { | |
988 | if (m_windowStyle & wxTE_MULTILINE) | |
989 | { | |
990 | // how ? | |
991 | } | |
992 | else | |
993 | { | |
994 | SetWidgetStyle(); | |
995 | gtk_widget_set_style( m_text, m_widgetStyle ); | |
996 | } | |
997 | } | |
998 | ||
999 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
1000 | { | |
1001 | Cut(); | |
1002 | } | |
1003 | ||
1004 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1005 | { | |
1006 | Copy(); | |
1007 | } | |
1008 | ||
1009 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1010 | { | |
1011 | Paste(); | |
1012 | } | |
1013 | ||
1014 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1015 | { | |
1016 | Undo(); | |
1017 | } | |
1018 | ||
1019 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1020 | { | |
1021 | Redo(); | |
1022 | } | |
1023 | ||
1024 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1025 | { | |
1026 | event.Enable( CanCut() ); | |
1027 | } | |
1028 | ||
1029 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1030 | { | |
1031 | event.Enable( CanCopy() ); | |
1032 | } | |
1033 | ||
1034 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1035 | { | |
1036 | event.Enable( CanPaste() ); | |
1037 | } | |
1038 | ||
1039 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1040 | { | |
1041 | event.Enable( CanUndo() ); | |
1042 | } | |
1043 | ||
1044 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1045 | { | |
1046 | event.Enable( CanRedo() ); | |
1047 | } |