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