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