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