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