]>
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/log.h" | |
18 | #include "wx/settings.h" | |
19 | #include "wx/panel.h" | |
20 | ||
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <ctype.h> | |
24 | #include <math.h> // for fabs | |
25 | ||
26 | // TODO: reimplement wxTextCtrl using GtkTextView | |
27 | #ifdef __WXGTK20__ | |
28 | #define GTK_ENABLE_BROKEN // need this to get GtkText at all | |
29 | #endif // __WXGTK20__ | |
30 | ||
31 | #include "wx/gtk/private.h" | |
32 | #include <gdk/gdkkeysyms.h> | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // idle system | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern void wxapp_install_idle_handler(); | |
39 | extern bool g_isIdle; | |
40 | ||
41 | //----------------------------------------------------------------------------- | |
42 | // data | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | extern bool g_blockEventsOnDrag; | |
46 | extern wxCursor g_globalCursor; | |
47 | extern wxWindowGTK *g_delayedFocus; | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // helpers | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | static void wxGtkTextInsert(GtkWidget *text, | |
54 | const wxTextAttr& attr, | |
55 | const char *txt, | |
56 | size_t len) | |
57 | { | |
58 | GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont() | |
59 | : NULL; | |
60 | ||
61 | GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor() | |
62 | : NULL; | |
63 | ||
64 | GdkColor *colBg = attr.HasBackgroundColour() | |
65 | ? attr.GetBackgroundColour().GetColor() | |
66 | : NULL; | |
67 | ||
68 | gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len ); | |
69 | } | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // "insert_text" for GtkEntry | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | static void | |
76 | gtk_insert_text_callback(GtkEditable *editable, | |
77 | const gchar *new_text, | |
78 | gint new_text_length, | |
79 | gint *position, | |
80 | wxTextCtrl *win) | |
81 | { | |
82 | if (g_isIdle) | |
83 | wxapp_install_idle_handler(); | |
84 | ||
85 | // we should only be called if we have a max len limit at all | |
86 | GtkEntry *entry = GTK_ENTRY (editable); | |
87 | ||
88 | wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") ); | |
89 | ||
90 | // check that we don't overflow the max length limit | |
91 | // | |
92 | // FIXME: this doesn't work when we paste a string which is going to be | |
93 | // truncated | |
94 | if ( entry->text_length == entry->text_max_length ) | |
95 | { | |
96 | // we don't need to run the base class version at all | |
97 | gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text"); | |
98 | ||
99 | // remember that the next changed signal is to be ignored to avoid | |
100 | // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event | |
101 | win->IgnoreNextTextUpdate(); | |
102 | ||
103 | // and generate the correct one ourselves | |
104 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId()); | |
105 | event.SetEventObject(win); | |
106 | event.SetString(win->GetValue()); | |
107 | win->GetEventHandler()->ProcessEvent( event ); | |
108 | } | |
109 | } | |
110 | ||
111 | //----------------------------------------------------------------------------- | |
112 | // "changed" | |
113 | //----------------------------------------------------------------------------- | |
114 | ||
115 | static void | |
116 | gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win ) | |
117 | { | |
118 | if ( win->IgnoreTextUpdate() ) | |
119 | return; | |
120 | ||
121 | if (!win->m_hasVMT) return; | |
122 | ||
123 | if (g_isIdle) | |
124 | wxapp_install_idle_handler(); | |
125 | ||
126 | win->SetModified(); | |
127 | win->UpdateFontIfNeeded(); | |
128 | ||
129 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); | |
130 | event.SetEventObject( win ); | |
131 | event.SetString( win->GetValue() ); | |
132 | win->GetEventHandler()->ProcessEvent( event ); | |
133 | } | |
134 | ||
135 | //----------------------------------------------------------------------------- | |
136 | // "changed" from vertical scrollbar | |
137 | //----------------------------------------------------------------------------- | |
138 | ||
139 | static void | |
140 | gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) | |
141 | { | |
142 | if (!win->m_hasVMT) return; | |
143 | ||
144 | if (g_isIdle) | |
145 | wxapp_install_idle_handler(); | |
146 | ||
147 | win->CalculateScrollbar(); | |
148 | } | |
149 | ||
150 | //----------------------------------------------------------------------------- | |
151 | // wxTextCtrl | |
152 | //----------------------------------------------------------------------------- | |
153 | ||
154 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
155 | ||
156 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
157 | EVT_CHAR(wxTextCtrl::OnChar) | |
158 | ||
159 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
160 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
161 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
162 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
163 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
164 | ||
165 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
166 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
167 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
168 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
169 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
170 | END_EVENT_TABLE() | |
171 | ||
172 | void wxTextCtrl::Init() | |
173 | { | |
174 | m_ignoreNextUpdate = | |
175 | m_modified = FALSE; | |
176 | m_updateFont = FALSE; | |
177 | m_text = | |
178 | m_vScrollbar = (GtkWidget *)NULL; | |
179 | } | |
180 | ||
181 | wxTextCtrl::wxTextCtrl( wxWindow *parent, | |
182 | wxWindowID id, | |
183 | const wxString &value, | |
184 | const wxPoint &pos, | |
185 | const wxSize &size, | |
186 | long style, | |
187 | const wxValidator& validator, | |
188 | const wxString &name ) | |
189 | { | |
190 | Init(); | |
191 | ||
192 | Create( parent, id, value, pos, size, style, validator, name ); | |
193 | } | |
194 | ||
195 | bool wxTextCtrl::Create( wxWindow *parent, | |
196 | wxWindowID id, | |
197 | const wxString &value, | |
198 | const wxPoint &pos, | |
199 | const wxSize &size, | |
200 | long style, | |
201 | const wxValidator& validator, | |
202 | const wxString &name ) | |
203 | { | |
204 | m_needParent = TRUE; | |
205 | m_acceptsFocus = TRUE; | |
206 | ||
207 | if (!PreCreation( parent, pos, size ) || | |
208 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
209 | { | |
210 | wxFAIL_MSG( wxT("wxTextCtrl creation failed") ); | |
211 | return FALSE; | |
212 | } | |
213 | ||
214 | ||
215 | m_vScrollbarVisible = FALSE; | |
216 | ||
217 | bool multi_line = (style & wxTE_MULTILINE) != 0; | |
218 | if (multi_line) | |
219 | { | |
220 | #ifdef __WXGTK13__ | |
221 | /* a multi-line edit control: create a vertical scrollbar by default and | |
222 | horizontal if requested */ | |
223 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; | |
224 | #else | |
225 | bool bHasHScrollbar = FALSE; | |
226 | #endif | |
227 | ||
228 | /* create our control ... */ | |
229 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); | |
230 | ||
231 | /* ... and put into the upper left hand corner of the table */ | |
232 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); | |
233 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
234 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, | |
235 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), | |
236 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), | |
237 | 0, 0); | |
238 | ||
239 | /* always wrap words */ | |
240 | gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE ); | |
241 | ||
242 | #ifdef __WXGTK13__ | |
243 | /* put the horizontal scrollbar in the lower left hand corner */ | |
244 | if (bHasHScrollbar) | |
245 | { | |
246 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); | |
247 | GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS ); | |
248 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, | |
249 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), | |
250 | GTK_FILL, | |
251 | 0, 0); | |
252 | gtk_widget_show(hscrollbar); | |
253 | ||
254 | /* don't wrap lines, otherwise we wouldn't need the scrollbar */ | |
255 | gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE ); | |
256 | } | |
257 | #endif | |
258 | ||
259 | /* finally, put the vertical scrollbar in the upper right corner */ | |
260 | m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj ); | |
261 | GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS ); | |
262 | gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1, | |
263 | GTK_FILL, | |
264 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), | |
265 | 0, 0); | |
266 | } | |
267 | else | |
268 | { | |
269 | /* a single-line text control: no need for scrollbars */ | |
270 | m_widget = | |
271 | m_text = gtk_entry_new(); | |
272 | } | |
273 | ||
274 | m_parent->DoAddChild( this ); | |
275 | ||
276 | m_focusWidget = m_text; | |
277 | ||
278 | PostCreation(); | |
279 | ||
280 | SetFont( parent->GetFont() ); | |
281 | ||
282 | wxSize size_best( DoGetBestSize() ); | |
283 | wxSize new_size( size ); | |
284 | if (new_size.x == -1) | |
285 | new_size.x = size_best.x; | |
286 | if (new_size.y == -1) | |
287 | new_size.y = size_best.y; | |
288 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
289 | SetSize( new_size.x, new_size.y ); | |
290 | ||
291 | if (multi_line) | |
292 | gtk_widget_show(m_text); | |
293 | ||
294 | if (multi_line) | |
295 | { | |
296 | gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed", | |
297 | (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this ); | |
298 | } | |
299 | ||
300 | if (!value.IsEmpty()) | |
301 | { | |
302 | gint tmp = 0; | |
303 | ||
304 | #if !GTK_CHECK_VERSION(1, 2, 0) | |
305 | // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in | |
306 | // gtk_editable_insert_text() | |
307 | gtk_widget_realize(m_text); | |
308 | #endif // GTK 1.0 | |
309 | ||
310 | #if wxUSE_UNICODE | |
311 | wxWX2MBbuf val = value.mbc_str(); | |
312 | gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp ); | |
313 | #else // !Unicode | |
314 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); | |
315 | #endif // Unicode/!Unicode | |
316 | ||
317 | if (multi_line) | |
318 | { | |
319 | /* bring editable's cursor uptodate. bug in GTK. */ | |
320 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); | |
321 | } | |
322 | } | |
323 | ||
324 | if (style & wxTE_PASSWORD) | |
325 | { | |
326 | if (!multi_line) | |
327 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
328 | } | |
329 | ||
330 | if (style & wxTE_READONLY) | |
331 | { | |
332 | if (!multi_line) | |
333 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
334 | } | |
335 | else | |
336 | { | |
337 | if (multi_line) | |
338 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
339 | } | |
340 | ||
341 | /* we want to be notified about text changes */ | |
342 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", | |
343 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
344 | ||
345 | /* we don't set a valid background colour, because the window | |
346 | manager should use a default one */ | |
347 | m_backgroundColour = wxColour(); | |
348 | ||
349 | wxColour colFg = parent->GetForegroundColour(); | |
350 | SetForegroundColour( colFg ); | |
351 | ||
352 | m_cursor = wxCursor( wxCURSOR_IBEAM ); | |
353 | ||
354 | wxTextAttr attrDef( colFg, m_backgroundColour, parent->GetFont() ); | |
355 | SetDefaultStyle( attrDef ); | |
356 | ||
357 | Show( TRUE ); | |
358 | ||
359 | return TRUE; | |
360 | } | |
361 | ||
362 | void wxTextCtrl::CalculateScrollbar() | |
363 | { | |
364 | if ((m_windowStyle & wxTE_MULTILINE) == 0) return; | |
365 | ||
366 | GtkAdjustment *adj = GTK_TEXT(m_text)->vadj; | |
367 | ||
368 | if (adj->upper - adj->page_size < 0.8) | |
369 | { | |
370 | if (m_vScrollbarVisible) | |
371 | { | |
372 | gtk_widget_hide( m_vScrollbar ); | |
373 | m_vScrollbarVisible = FALSE; | |
374 | } | |
375 | } | |
376 | else | |
377 | { | |
378 | if (!m_vScrollbarVisible) | |
379 | { | |
380 | gtk_widget_show( m_vScrollbar ); | |
381 | m_vScrollbarVisible = TRUE; | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | wxString wxTextCtrl::GetValue() const | |
387 | { | |
388 | wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") ); | |
389 | ||
390 | wxString tmp; | |
391 | if (m_windowStyle & wxTE_MULTILINE) | |
392 | { | |
393 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
394 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
395 | tmp = wxString(text,*wxConvCurrent); | |
396 | g_free( text ); | |
397 | } | |
398 | else | |
399 | { | |
400 | tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent); | |
401 | } | |
402 | return tmp; | |
403 | } | |
404 | ||
405 | void wxTextCtrl::SetValue( const wxString &value ) | |
406 | { | |
407 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
408 | ||
409 | if (m_windowStyle & wxTE_MULTILINE) | |
410 | { | |
411 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
412 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
413 | len = 0; | |
414 | #if wxUSE_UNICODE | |
415 | wxWX2MBbuf tmpbuf = value.mbc_str(); | |
416 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len ); | |
417 | #else | |
418 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len ); | |
419 | #endif | |
420 | } | |
421 | else | |
422 | { | |
423 | gtk_entry_set_text( GTK_ENTRY(m_text), value.mbc_str() ); | |
424 | } | |
425 | ||
426 | // GRG, Jun/2000: Changed this after a lot of discussion in | |
427 | // the lists. wxWindows 2.2 will have a set of flags to | |
428 | // customize this behaviour. | |
429 | SetInsertionPoint(0); | |
430 | ||
431 | m_modified = FALSE; | |
432 | } | |
433 | ||
434 | void wxTextCtrl::WriteText( const wxString &text ) | |
435 | { | |
436 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
437 | ||
438 | if ( text.empty() ) | |
439 | return; | |
440 | ||
441 | #if wxUSE_UNICODE | |
442 | wxWX2MBbuf buf = text.mbc_str(); | |
443 | const char *txt = buf; | |
444 | size_t txtlen = strlen(buf); | |
445 | #else | |
446 | const char *txt = text; | |
447 | size_t txtlen = text.length(); | |
448 | #endif | |
449 | ||
450 | if ( m_windowStyle & wxTE_MULTILINE ) | |
451 | { | |
452 | // After cursor movements, gtk_text_get_point() is wrong by one. | |
453 | gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) ); | |
454 | ||
455 | // always use m_defaultStyle, even if it is empty as otherwise | |
456 | // resetting the style and appending some more text wouldn't work: if | |
457 | // we don't specify the style explicitly, the old style would be used | |
458 | wxGtkTextInsert(m_text, m_defaultStyle, txt, txtlen); | |
459 | ||
460 | // Bring editable's cursor back uptodate. | |
461 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); | |
462 | } | |
463 | else // single line | |
464 | { | |
465 | // This moves the cursor pos to behind the inserted text. | |
466 | gint len = GET_EDITABLE_POS(m_text); | |
467 | gtk_editable_insert_text( GTK_EDITABLE(m_text), txt, txtlen, &len ); | |
468 | ||
469 | // Bring editable's cursor uptodate. | |
470 | len += text.Len(); | |
471 | ||
472 | // Bring entry's cursor uptodate. | |
473 | gtk_entry_set_position( GTK_ENTRY(m_text), len ); | |
474 | } | |
475 | ||
476 | m_modified = TRUE; | |
477 | } | |
478 | ||
479 | void wxTextCtrl::AppendText( const wxString &text ) | |
480 | { | |
481 | SetInsertionPointEnd(); | |
482 | WriteText( text ); | |
483 | } | |
484 | ||
485 | wxString wxTextCtrl::GetLineText( long lineNo ) const | |
486 | { | |
487 | if (m_windowStyle & wxTE_MULTILINE) | |
488 | { | |
489 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
490 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
491 | ||
492 | if (text) | |
493 | { | |
494 | wxString buf(wxT("")); | |
495 | long i; | |
496 | int currentLine = 0; | |
497 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
498 | if (text[i] == '\n') | |
499 | currentLine++; | |
500 | // Now get the text | |
501 | int j; | |
502 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
503 | buf += text[i]; | |
504 | ||
505 | g_free( text ); | |
506 | return buf; | |
507 | } | |
508 | else | |
509 | { | |
510 | return wxEmptyString; | |
511 | } | |
512 | } | |
513 | else | |
514 | { | |
515 | if (lineNo == 0) return GetValue(); | |
516 | return wxEmptyString; | |
517 | } | |
518 | } | |
519 | ||
520 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) | |
521 | { | |
522 | /* If you implement this, don't forget to update the documentation! | |
523 | * (file docs/latex/wx/text.tex) */ | |
524 | wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") ); | |
525 | } | |
526 | ||
527 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const | |
528 | { | |
529 | if ( m_windowStyle & wxTE_MULTILINE ) | |
530 | { | |
531 | wxString text = GetValue(); | |
532 | ||
533 | // cast to prevent warning. But pos really should've been unsigned. | |
534 | if( (unsigned long)pos > text.Len() ) | |
535 | return FALSE; | |
536 | ||
537 | *x=0; // First Col | |
538 | *y=0; // First Line | |
539 | ||
540 | const wxChar* stop = text.c_str() + pos; | |
541 | for ( const wxChar *p = text.c_str(); p < stop; p++ ) | |
542 | { | |
543 | if (*p == wxT('\n')) | |
544 | { | |
545 | (*y)++; | |
546 | *x=0; | |
547 | } | |
548 | else | |
549 | (*x)++; | |
550 | } | |
551 | } | |
552 | else // single line control | |
553 | { | |
554 | if ( pos <= GTK_ENTRY(m_text)->text_length ) | |
555 | { | |
556 | *y = 0; | |
557 | *x = pos; | |
558 | } | |
559 | else | |
560 | { | |
561 | // index out of bounds | |
562 | return FALSE; | |
563 | } | |
564 | } | |
565 | ||
566 | return TRUE; | |
567 | } | |
568 | ||
569 | long wxTextCtrl::XYToPosition(long x, long y ) const | |
570 | { | |
571 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; | |
572 | ||
573 | long pos=0; | |
574 | for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n' | |
575 | ||
576 | pos += x; | |
577 | return pos; | |
578 | } | |
579 | ||
580 | int wxTextCtrl::GetLineLength(long lineNo) const | |
581 | { | |
582 | wxString str = GetLineText (lineNo); | |
583 | return (int) str.Length(); | |
584 | } | |
585 | ||
586 | int wxTextCtrl::GetNumberOfLines() const | |
587 | { | |
588 | if (m_windowStyle & wxTE_MULTILINE) | |
589 | { | |
590 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
591 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
592 | ||
593 | if (text) | |
594 | { | |
595 | int currentLine = 0; | |
596 | for (int i = 0; i < len; i++ ) | |
597 | { | |
598 | if (text[i] == '\n') | |
599 | currentLine++; | |
600 | } | |
601 | g_free( text ); | |
602 | ||
603 | // currentLine is 0 based, add 1 to get number of lines | |
604 | return currentLine + 1; | |
605 | } | |
606 | else | |
607 | { | |
608 | return 0; | |
609 | } | |
610 | } | |
611 | else | |
612 | { | |
613 | return 1; | |
614 | } | |
615 | } | |
616 | ||
617 | void wxTextCtrl::SetInsertionPoint( long pos ) | |
618 | { | |
619 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
620 | ||
621 | if (m_windowStyle & wxTE_MULTILINE) | |
622 | { | |
623 | /* seems to be broken in GTK 1.0.X: | |
624 | gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */ | |
625 | ||
626 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_text), | |
627 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
628 | ||
629 | /* we fake a set_point by inserting and deleting. as the user | |
630 | isn't supposed to get to know about thos non-sense, we | |
631 | disconnect so that no events are sent to the user program. */ | |
632 | ||
633 | gint tmp = (gint)pos; | |
634 | gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp ); | |
635 | gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp ); | |
636 | ||
637 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", | |
638 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
639 | ||
640 | /* bring editable's cursor uptodate. another bug in GTK. */ | |
641 | ||
642 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); | |
643 | } | |
644 | else | |
645 | { | |
646 | gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos ); | |
647 | ||
648 | /* bring editable's cursor uptodate. bug in GTK. */ | |
649 | ||
650 | SET_EDITABLE_POS(m_text, (guint32)pos); | |
651 | } | |
652 | } | |
653 | ||
654 | void wxTextCtrl::SetInsertionPointEnd() | |
655 | { | |
656 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
657 | ||
658 | if (m_windowStyle & wxTE_MULTILINE) | |
659 | SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text))); | |
660 | else | |
661 | gtk_entry_set_position( GTK_ENTRY(m_text), -1 ); | |
662 | } | |
663 | ||
664 | void wxTextCtrl::SetEditable( bool editable ) | |
665 | { | |
666 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
667 | ||
668 | if (m_windowStyle & wxTE_MULTILINE) | |
669 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); | |
670 | else | |
671 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); | |
672 | } | |
673 | ||
674 | bool wxTextCtrl::Enable( bool enable ) | |
675 | { | |
676 | if (!wxWindowBase::Enable(enable)) | |
677 | { | |
678 | // nothing to do | |
679 | return FALSE; | |
680 | } | |
681 | ||
682 | if (m_windowStyle & wxTE_MULTILINE) | |
683 | { | |
684 | gtk_text_set_editable( GTK_TEXT(m_text), enable ); | |
685 | OnParentEnable(enable); | |
686 | } | |
687 | else | |
688 | { | |
689 | gtk_widget_set_sensitive( m_text, enable ); | |
690 | } | |
691 | ||
692 | return TRUE; | |
693 | } | |
694 | ||
695 | // wxGTK-specific: called recursively by Enable, | |
696 | // to give widgets an oppprtunity to correct their colours after they | |
697 | // have been changed by Enable | |
698 | void wxTextCtrl::OnParentEnable( bool enable ) | |
699 | { | |
700 | // If we have a custom background colour, we use this colour in both | |
701 | // disabled and enabled mode, or we end up with a different colour under the | |
702 | // text. | |
703 | wxColour oldColour = GetBackgroundColour(); | |
704 | if (oldColour.Ok()) | |
705 | { | |
706 | // Need to set twice or it'll optimize the useful stuff out | |
707 | if (oldColour == * wxWHITE) | |
708 | SetBackgroundColour(*wxBLACK); | |
709 | else | |
710 | SetBackgroundColour(*wxWHITE); | |
711 | SetBackgroundColour(oldColour); | |
712 | } | |
713 | } | |
714 | ||
715 | void wxTextCtrl::DiscardEdits() | |
716 | { | |
717 | m_modified = FALSE; | |
718 | } | |
719 | ||
720 | // ---------------------------------------------------------------------------- | |
721 | // max text length support | |
722 | // ---------------------------------------------------------------------------- | |
723 | ||
724 | void wxTextCtrl::IgnoreNextTextUpdate() | |
725 | { | |
726 | m_ignoreNextUpdate = TRUE; | |
727 | } | |
728 | ||
729 | bool wxTextCtrl::IgnoreTextUpdate() | |
730 | { | |
731 | if ( m_ignoreNextUpdate ) | |
732 | { | |
733 | m_ignoreNextUpdate = FALSE; | |
734 | ||
735 | return TRUE; | |
736 | } | |
737 | ||
738 | return FALSE; | |
739 | } | |
740 | ||
741 | void wxTextCtrl::SetMaxLength(unsigned long len) | |
742 | { | |
743 | if ( !HasFlag(wxTE_MULTILINE) ) | |
744 | { | |
745 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
746 | ||
747 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
748 | // we had tried to enter more text than allowed by max text length and | |
749 | // the text wasn't really changed | |
750 | // | |
751 | // to detect this and generate TEXT_MAXLEN event instead of | |
752 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
753 | // | |
754 | // when max len is set to 0 we disconnect our handler as it means that | |
755 | // we shouldn't check anything any more | |
756 | if ( len ) | |
757 | { | |
758 | gtk_signal_connect( GTK_OBJECT(m_text), | |
759 | "insert_text", | |
760 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
761 | (gpointer)this); | |
762 | } | |
763 | else // no checking | |
764 | { | |
765 | gtk_signal_disconnect_by_func | |
766 | ( | |
767 | GTK_OBJECT(m_text), | |
768 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
769 | (gpointer)this | |
770 | ); | |
771 | } | |
772 | } | |
773 | } | |
774 | ||
775 | void wxTextCtrl::SetSelection( long from, long to ) | |
776 | { | |
777 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
778 | ||
779 | if ( (m_windowStyle & wxTE_MULTILINE) && | |
780 | !GTK_TEXT(m_text)->line_start_cache ) | |
781 | { | |
782 | // tell the programmer that it didn't work | |
783 | wxLogDebug(_T("Can't call SetSelection() before realizing the control")); | |
784 | return; | |
785 | } | |
786 | ||
787 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
788 | } | |
789 | ||
790 | void wxTextCtrl::ShowPosition( long pos ) | |
791 | { | |
792 | if (m_windowStyle & wxTE_MULTILINE) | |
793 | { | |
794 | GtkAdjustment *vp = GTK_TEXT(m_text)->vadj; | |
795 | float totalLines = (float) GetNumberOfLines(); | |
796 | long posX; | |
797 | long posY; | |
798 | PositionToXY(pos, &posX, &posY); | |
799 | float posLine = (float) posY; | |
800 | float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower; | |
801 | gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p); | |
802 | } | |
803 | } | |
804 | ||
805 | long wxTextCtrl::GetInsertionPoint() const | |
806 | { | |
807 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); | |
808 | ||
809 | return (long) GET_EDITABLE_POS(m_text); | |
810 | } | |
811 | ||
812 | long wxTextCtrl::GetLastPosition() const | |
813 | { | |
814 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); | |
815 | ||
816 | int pos = 0; | |
817 | if (m_windowStyle & wxTE_MULTILINE) | |
818 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); | |
819 | else | |
820 | pos = GTK_ENTRY(m_text)->text_length; | |
821 | ||
822 | return (long)pos; | |
823 | } | |
824 | ||
825 | void wxTextCtrl::Remove( long from, long to ) | |
826 | { | |
827 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
828 | ||
829 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
830 | } | |
831 | ||
832 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) | |
833 | { | |
834 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
835 | ||
836 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
837 | ||
838 | if (!value.IsEmpty()) | |
839 | { | |
840 | gint pos = (gint)from; | |
841 | #if wxUSE_UNICODE | |
842 | wxWX2MBbuf buf = value.mbc_str(); | |
843 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos ); | |
844 | #else | |
845 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); | |
846 | #endif | |
847 | } | |
848 | } | |
849 | ||
850 | void wxTextCtrl::Cut() | |
851 | { | |
852 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
853 | ||
854 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG ); | |
855 | } | |
856 | ||
857 | void wxTextCtrl::Copy() | |
858 | { | |
859 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
860 | ||
861 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG ); | |
862 | } | |
863 | ||
864 | void wxTextCtrl::Paste() | |
865 | { | |
866 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
867 | ||
868 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG ); | |
869 | } | |
870 | ||
871 | // Undo/redo | |
872 | void wxTextCtrl::Undo() | |
873 | { | |
874 | // TODO | |
875 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); | |
876 | } | |
877 | ||
878 | void wxTextCtrl::Redo() | |
879 | { | |
880 | // TODO | |
881 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); | |
882 | } | |
883 | ||
884 | bool wxTextCtrl::CanUndo() const | |
885 | { | |
886 | // TODO | |
887 | wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); | |
888 | return FALSE; | |
889 | } | |
890 | ||
891 | bool wxTextCtrl::CanRedo() const | |
892 | { | |
893 | // TODO | |
894 | wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); | |
895 | return FALSE; | |
896 | } | |
897 | ||
898 | // If the return values from and to are the same, there is no | |
899 | // selection. | |
900 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const | |
901 | { | |
902 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
903 | ||
904 | gint from, to; | |
905 | #ifdef __WXGTK20__ | |
906 | if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text), &from, &to) ) | |
907 | #else | |
908 | if ( !(GTK_EDITABLE(m_text)->has_selection) ) | |
909 | #endif | |
910 | { | |
911 | from = | |
912 | to = GetInsertionPoint(); | |
913 | } | |
914 | else // got selection | |
915 | { | |
916 | #ifndef __WXGTK20__ | |
917 | from = (long) GTK_EDITABLE(m_text)->selection_start_pos; | |
918 | to = (long) GTK_EDITABLE(m_text)->selection_end_pos; | |
919 | #endif | |
920 | ||
921 | if ( from > to ) | |
922 | { | |
923 | // exchange them to be compatible with wxMSW | |
924 | gint tmp = from; | |
925 | from = to; | |
926 | to = tmp; | |
927 | } | |
928 | } | |
929 | ||
930 | if ( fromOut ) | |
931 | *fromOut = from; | |
932 | if ( toOut ) | |
933 | *toOut = to; | |
934 | } | |
935 | ||
936 | bool wxTextCtrl::IsEditable() const | |
937 | { | |
938 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); | |
939 | ||
940 | #ifdef __WXGTK20__ | |
941 | return gtk_editable_get_editable(GTK_EDITABLE(m_text)); | |
942 | #else | |
943 | return GTK_EDITABLE(m_text)->editable; | |
944 | #endif | |
945 | } | |
946 | ||
947 | bool wxTextCtrl::IsModified() const | |
948 | { | |
949 | return m_modified; | |
950 | } | |
951 | ||
952 | void wxTextCtrl::Clear() | |
953 | { | |
954 | SetValue( wxT("") ); | |
955 | } | |
956 | ||
957 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) | |
958 | { | |
959 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); | |
960 | ||
961 | if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) | |
962 | { | |
963 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
964 | event.SetEventObject(this); | |
965 | event.SetString(GetValue()); | |
966 | if (GetEventHandler()->ProcessEvent(event)) return; | |
967 | } | |
968 | ||
969 | if ((key_event.KeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE)) | |
970 | { | |
971 | // This will invoke the dialog default action, such | |
972 | // as the clicking the default button. | |
973 | ||
974 | wxWindow *top_frame = m_parent; | |
975 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
976 | top_frame = top_frame->GetParent(); | |
977 | ||
978 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
979 | { | |
980 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
981 | ||
982 | if (window->default_widget) | |
983 | { | |
984 | gtk_widget_activate (window->default_widget); | |
985 | return; | |
986 | } | |
987 | } | |
988 | } | |
989 | ||
990 | key_event.Skip(); | |
991 | } | |
992 | ||
993 | GtkWidget* wxTextCtrl::GetConnectWidget() | |
994 | { | |
995 | return GTK_WIDGET(m_text); | |
996 | } | |
997 | ||
998 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) | |
999 | { | |
1000 | if (m_windowStyle & wxTE_MULTILINE) | |
1001 | return (window == GTK_TEXT(m_text)->text_area); | |
1002 | else | |
1003 | return (window == GTK_ENTRY(m_text)->text_area); | |
1004 | } | |
1005 | ||
1006 | // the font will change for subsequent text insertiongs | |
1007 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
1008 | { | |
1009 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); | |
1010 | ||
1011 | if ( !wxTextCtrlBase::SetFont(font) ) | |
1012 | { | |
1013 | // font didn't change, nothing to do | |
1014 | return FALSE; | |
1015 | } | |
1016 | ||
1017 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1018 | { | |
1019 | m_updateFont = TRUE; | |
1020 | ||
1021 | m_defaultStyle.SetFont(font); | |
1022 | ||
1023 | ChangeFontGlobally(); | |
1024 | } | |
1025 | ||
1026 | return TRUE; | |
1027 | } | |
1028 | ||
1029 | void wxTextCtrl::ChangeFontGlobally() | |
1030 | { | |
1031 | // this method is very inefficient and hence should be called as rarely as | |
1032 | // possible! | |
1033 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont, | |
1034 | _T("shouldn't be called for single line controls") ); | |
1035 | ||
1036 | wxString value = GetValue(); | |
1037 | if ( !value.IsEmpty() ) | |
1038 | { | |
1039 | m_updateFont = FALSE; | |
1040 | ||
1041 | Clear(); | |
1042 | AppendText(value); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | void wxTextCtrl::UpdateFontIfNeeded() | |
1047 | { | |
1048 | if ( m_updateFont ) | |
1049 | ChangeFontGlobally(); | |
1050 | } | |
1051 | ||
1052 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
1053 | { | |
1054 | if ( !wxControl::SetForegroundColour(colour) ) | |
1055 | return FALSE; | |
1056 | ||
1057 | // update default fg colour too | |
1058 | m_defaultStyle.SetTextColour(colour); | |
1059 | ||
1060 | return TRUE; | |
1061 | } | |
1062 | ||
1063 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) | |
1064 | { | |
1065 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); | |
1066 | ||
1067 | if ( !wxControl::SetBackgroundColour( colour ) ) | |
1068 | return FALSE; | |
1069 | ||
1070 | if (!m_widget->window) | |
1071 | return FALSE; | |
1072 | ||
1073 | wxColour sysbg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ); | |
1074 | if (sysbg.Red() == colour.Red() && | |
1075 | sysbg.Green() == colour.Green() && | |
1076 | sysbg.Blue() == colour.Blue()) | |
1077 | { | |
1078 | return FALSE; // FIXME or TRUE? | |
1079 | } | |
1080 | ||
1081 | if (!m_backgroundColour.Ok()) | |
1082 | return FALSE; | |
1083 | ||
1084 | if (m_windowStyle & wxTE_MULTILINE) | |
1085 | { | |
1086 | GdkWindow *window = GTK_TEXT(m_text)->text_area; | |
1087 | if (!window) | |
1088 | return FALSE; | |
1089 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); | |
1090 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
1091 | gdk_window_clear( window ); | |
1092 | } | |
1093 | ||
1094 | // change active background color too | |
1095 | m_defaultStyle.SetBackgroundColour( colour ); | |
1096 | ||
1097 | return TRUE; | |
1098 | } | |
1099 | ||
1100 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) | |
1101 | { | |
1102 | /* VERY dirty way to do that - removes the required text and re-adds it | |
1103 | with styling (FIXME) */ | |
1104 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1105 | { | |
1106 | if ( style.IsDefault() ) | |
1107 | { | |
1108 | // nothing to do | |
1109 | return TRUE; | |
1110 | } | |
1111 | ||
1112 | gint l = gtk_text_get_length( GTK_TEXT(m_text) ); | |
1113 | ||
1114 | wxCHECK_MSG( start >= 0 && end <= l, FALSE, | |
1115 | _T("invalid range in wxTextCtrl::SetStyle") ); | |
1116 | ||
1117 | gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) ); | |
1118 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end ); | |
1119 | wxString tmp(text,*wxConvCurrent); | |
1120 | g_free( text ); | |
1121 | ||
1122 | gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end ); | |
1123 | gtk_editable_set_position( GTK_EDITABLE(m_text), start ); | |
1124 | ||
1125 | #if wxUSE_UNICODE | |
1126 | wxWX2MBbuf buf = tmp.mbc_str(); | |
1127 | const char *txt = buf; | |
1128 | size_t txtlen = strlen(buf); | |
1129 | #else | |
1130 | const char *txt = tmp; | |
1131 | size_t txtlen = tmp.length(); | |
1132 | #endif | |
1133 | ||
1134 | // use the attributes from style which are set in it and fall back | |
1135 | // first to the default style and then to the text control default | |
1136 | // colours for the others | |
1137 | wxGtkTextInsert(m_text, | |
1138 | wxTextAttr::Combine(style, m_defaultStyle, this), | |
1139 | txt, | |
1140 | txtlen); | |
1141 | ||
1142 | /* does not seem to help under GTK+ 1.2 !!! | |
1143 | gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */ | |
1144 | SetInsertionPoint( old_pos ); | |
1145 | return TRUE; | |
1146 | } | |
1147 | else // singe line | |
1148 | { | |
1149 | // cannot do this for GTK+'s Entry widget | |
1150 | return FALSE; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | void wxTextCtrl::ApplyWidgetStyle() | |
1155 | { | |
1156 | if (m_windowStyle & wxTE_MULTILINE) | |
1157 | { | |
1158 | // how ? | |
1159 | } | |
1160 | else | |
1161 | { | |
1162 | SetWidgetStyle(); | |
1163 | gtk_widget_set_style( m_text, m_widgetStyle ); | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
1168 | { | |
1169 | Cut(); | |
1170 | } | |
1171 | ||
1172 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1173 | { | |
1174 | Copy(); | |
1175 | } | |
1176 | ||
1177 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1178 | { | |
1179 | Paste(); | |
1180 | } | |
1181 | ||
1182 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1183 | { | |
1184 | Undo(); | |
1185 | } | |
1186 | ||
1187 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1188 | { | |
1189 | Redo(); | |
1190 | } | |
1191 | ||
1192 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1193 | { | |
1194 | event.Enable( CanCut() ); | |
1195 | } | |
1196 | ||
1197 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1198 | { | |
1199 | event.Enable( CanCopy() ); | |
1200 | } | |
1201 | ||
1202 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1203 | { | |
1204 | event.Enable( CanPaste() ); | |
1205 | } | |
1206 | ||
1207 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1208 | { | |
1209 | event.Enable( CanUndo() ); | |
1210 | } | |
1211 | ||
1212 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1213 | { | |
1214 | event.Enable( CanRedo() ); | |
1215 | } | |
1216 | ||
1217 | void wxTextCtrl::OnInternalIdle() | |
1218 | { | |
1219 | wxCursor cursor = m_cursor; | |
1220 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
1221 | ||
1222 | if (cursor.Ok()) | |
1223 | { | |
1224 | GdkWindow *window = (GdkWindow*) NULL; | |
1225 | if (HasFlag(wxTE_MULTILINE)) | |
1226 | window = GTK_TEXT(m_text)->text_area; | |
1227 | else | |
1228 | window = GTK_ENTRY(m_text)->text_area; | |
1229 | ||
1230 | if (window) | |
1231 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
1232 | ||
1233 | if (!g_globalCursor.Ok()) | |
1234 | cursor = *wxSTANDARD_CURSOR; | |
1235 | ||
1236 | window = m_widget->window; | |
1237 | if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget))) | |
1238 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
1239 | } | |
1240 | ||
1241 | if (g_delayedFocus == this) | |
1242 | { | |
1243 | if (GTK_WIDGET_REALIZED(m_widget)) | |
1244 | { | |
1245 | gtk_widget_grab_focus( m_widget ); | |
1246 | g_delayedFocus = NULL; | |
1247 | } | |
1248 | } | |
1249 | ||
1250 | UpdateWindowUI(); | |
1251 | } | |
1252 | ||
1253 | wxSize wxTextCtrl::DoGetBestSize() const | |
1254 | { | |
1255 | // FIXME should be different for multi-line controls... | |
1256 | wxSize ret( wxControl::DoGetBestSize() ); | |
1257 | return wxSize(80, ret.y); | |
1258 | } | |
1259 | ||
1260 | // ---------------------------------------------------------------------------- | |
1261 | // freeze/thaw | |
1262 | // ---------------------------------------------------------------------------- | |
1263 | ||
1264 | void wxTextCtrl::Freeze() | |
1265 | { | |
1266 | if ( HasFlag(wxTE_MULTILINE) ) | |
1267 | { | |
1268 | gtk_text_freeze(GTK_TEXT(m_text)); | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | void wxTextCtrl::Thaw() | |
1273 | { | |
1274 | if ( HasFlag(wxTE_MULTILINE) ) | |
1275 | { | |
1276 | GTK_TEXT(m_text)->vadj->value = 0.0; | |
1277 | ||
1278 | gtk_text_thaw(GTK_TEXT(m_text)); | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | // ---------------------------------------------------------------------------- | |
1283 | // scrolling | |
1284 | // ---------------------------------------------------------------------------- | |
1285 | ||
1286 | GtkAdjustment *wxTextCtrl::GetVAdj() const | |
1287 | { | |
1288 | return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL; | |
1289 | } | |
1290 | ||
1291 | bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff) | |
1292 | { | |
1293 | float value = adj->value + diff; | |
1294 | ||
1295 | if ( value < 0 ) | |
1296 | value = 0; | |
1297 | ||
1298 | float upper = adj->upper - adj->page_size; | |
1299 | if ( value > upper ) | |
1300 | value = upper; | |
1301 | ||
1302 | // did we noticeably change the scroll position? | |
1303 | if ( fabs(adj->value - value) < 0.2 ) | |
1304 | { | |
1305 | // well, this is what Robert does in wxScrollBar, so it must be good... | |
1306 | return FALSE; | |
1307 | } | |
1308 | ||
1309 | adj->value = value; | |
1310 | ||
1311 | gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed"); | |
1312 | ||
1313 | return TRUE; | |
1314 | } | |
1315 | ||
1316 | bool wxTextCtrl::ScrollLines(int lines) | |
1317 | { | |
1318 | GtkAdjustment *adj = GetVAdj(); | |
1319 | if ( !adj ) | |
1320 | return FALSE; | |
1321 | ||
1322 | // this is hardcoded to 10 in GTK+ 1.2 (great idea) | |
1323 | static const int KEY_SCROLL_PIXELS = 10; | |
1324 | ||
1325 | return DoScroll(adj, lines*KEY_SCROLL_PIXELS); | |
1326 | } | |
1327 | ||
1328 | bool wxTextCtrl::ScrollPages(int pages) | |
1329 | { | |
1330 | GtkAdjustment *adj = GetVAdj(); | |
1331 | if ( !adj ) | |
1332 | return FALSE; | |
1333 | ||
1334 | return DoScroll(adj, (int)ceil(pages*adj->page_increment)); | |
1335 | } | |
1336 |