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