]>
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 |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
11 | #pragma implementation "textctrl.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
c801d85f KB |
17 | #include "wx/textctrl.h" |
18 | #include "wx/utils.h" | |
ae0bdb01 | 19 | #include "wx/intl.h" |
a1f79c1e | 20 | #include "wx/log.h" |
ae0bdb01 | 21 | #include "wx/settings.h" |
fc71ef6e | 22 | #include "wx/panel.h" |
fab591c5 | 23 | #include "wx/strconv.h" |
cc3da3f8 | 24 | #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo()) |
c801d85f | 25 | |
a81258be RR |
26 | #include <sys/types.h> |
27 | #include <sys/stat.h> | |
28 | #include <ctype.h> | |
817ec43a | 29 | #include <math.h> // for fabs |
a81258be | 30 | |
9e691f46 VZ |
31 | #include "wx/gtk/private.h" |
32 | #include <gdk/gdkkeysyms.h> | |
b292e2f5 | 33 | |
acfd422a RR |
34 | //----------------------------------------------------------------------------- |
35 | // idle system | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern void wxapp_install_idle_handler(); | |
39 | extern bool g_isIdle; | |
40 | ||
b292e2f5 RR |
41 | //----------------------------------------------------------------------------- |
42 | // data | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
65045edd RR |
45 | extern bool g_blockEventsOnDrag; |
46 | extern wxCursor g_globalCursor; | |
d7fa7eaa | 47 | extern wxWindowGTK *g_delayedFocus; |
b292e2f5 | 48 | |
eda40bfc VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // helpers | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
cc3da3f8 RR |
53 | #ifdef __WXGTK20__ |
54 | static void wxGtkTextInsert(GtkWidget *text, | |
55 | GtkTextBuffer *text_buffer, | |
56 | const wxTextAttr& attr, | |
57 | wxCharBuffer buffer) | |
58 | ||
59 | { | |
60 | PangoFontDescription *font_description = attr.HasFont() | |
61 | ? attr.GetFont().GetNativeFontInfo()->description | |
62 | : NULL; | |
63 | ||
64 | GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor() | |
65 | : NULL; | |
66 | ||
67 | GdkColor *colBg = attr.HasBackgroundColour() | |
68 | ? attr.GetBackgroundColour().GetColor() | |
69 | : NULL; | |
70 | ||
cc3da3f8 RR |
71 | GtkTextTag *tag; |
72 | tag = gtk_text_buffer_create_tag( text_buffer, NULL, "font-desc", font_description, | |
73 | "foreground-gdk", colFg, | |
74 | "background-gdk", colBg, NULL ); | |
a61bbf87 JS |
75 | |
76 | GtkTextIter iter; | |
77 | gtk_text_buffer_get_iter_at_mark( text_buffer, &iter, | |
78 | gtk_text_buffer_get_insert (text_buffer) ); | |
79 | ||
80 | gtk_text_buffer_insert_with_tags( text_buffer, &iter, buffer, strlen(buffer), tag, NULL ); | |
cc3da3f8 RR |
81 | } |
82 | #else | |
eda40bfc VZ |
83 | static void wxGtkTextInsert(GtkWidget *text, |
84 | const wxTextAttr& attr, | |
85 | const char *txt, | |
86 | size_t len) | |
87 | { | |
88 | GdkFont *font = attr.HasFont() ? attr.GetFont().GetInternalFont() | |
89 | : NULL; | |
90 | ||
91 | GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor() | |
92 | : NULL; | |
93 | ||
94 | GdkColor *colBg = attr.HasBackgroundColour() | |
95 | ? attr.GetBackgroundColour().GetColor() | |
96 | : NULL; | |
97 | ||
98 | gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len ); | |
99 | } | |
a8bf1826 | 100 | #endif // GTK 1.x |
eda40bfc | 101 | |
ce2f50e3 VZ |
102 | // ---------------------------------------------------------------------------- |
103 | // "insert_text" for GtkEntry | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | static void | |
107 | gtk_insert_text_callback(GtkEditable *editable, | |
108 | const gchar *new_text, | |
109 | gint new_text_length, | |
110 | gint *position, | |
111 | wxTextCtrl *win) | |
112 | { | |
76fcf0f2 RR |
113 | if (g_isIdle) |
114 | wxapp_install_idle_handler(); | |
115 | ||
ce2f50e3 VZ |
116 | // we should only be called if we have a max len limit at all |
117 | GtkEntry *entry = GTK_ENTRY (editable); | |
118 | ||
119 | wxCHECK_RET( entry->text_max_length, _T("shouldn't be called") ); | |
120 | ||
121 | // check that we don't overflow the max length limit | |
122 | // | |
123 | // FIXME: this doesn't work when we paste a string which is going to be | |
124 | // truncated | |
125 | if ( entry->text_length == entry->text_max_length ) | |
126 | { | |
127 | // we don't need to run the base class version at all | |
128 | gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text"); | |
129 | ||
130 | // remember that the next changed signal is to be ignored to avoid | |
131 | // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event | |
132 | win->IgnoreNextTextUpdate(); | |
133 | ||
134 | // and generate the correct one ourselves | |
135 | wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, win->GetId()); | |
136 | event.SetEventObject(win); | |
137 | event.SetString(win->GetValue()); | |
138 | win->GetEventHandler()->ProcessEvent( event ); | |
139 | } | |
140 | } | |
141 | ||
c801d85f | 142 | //----------------------------------------------------------------------------- |
2f2aa628 | 143 | // "changed" |
c801d85f KB |
144 | //----------------------------------------------------------------------------- |
145 | ||
805dd538 | 146 | static void |
ba3f6b44 | 147 | gtk_text_changed_callback( GtkWidget *widget, wxTextCtrl *win ) |
484e45bf | 148 | { |
ce2f50e3 VZ |
149 | if ( win->IgnoreTextUpdate() ) |
150 | return; | |
151 | ||
a2053b27 | 152 | if (!win->m_hasVMT) return; |
805dd538 | 153 | |
bb69661b | 154 | if (g_isIdle) |
3c679789 | 155 | wxapp_install_idle_handler(); |
a8bf1826 | 156 | |
034be888 | 157 | win->SetModified(); |
c04ec496 | 158 | #ifndef __WXGTK20__ |
01041145 | 159 | win->UpdateFontIfNeeded(); |
c04ec496 | 160 | #endif // !__WXGTK20__ |
a8bf1826 | 161 | |
f03fc89f | 162 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); |
2830bf19 | 163 | event.SetEventObject( win ); |
ce2f50e3 | 164 | event.SetString( win->GetValue() ); |
2830bf19 | 165 | win->GetEventHandler()->ProcessEvent( event ); |
6de97a3b | 166 | } |
112892b9 | 167 | |
2830bf19 | 168 | //----------------------------------------------------------------------------- |
034be888 | 169 | // "changed" from vertical scrollbar |
2830bf19 RR |
170 | //----------------------------------------------------------------------------- |
171 | ||
fab591c5 | 172 | #ifndef __WXGTK20__ |
805dd538 | 173 | static void |
034be888 | 174 | gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
2830bf19 | 175 | { |
3c679789 | 176 | if (!win->m_hasVMT) return; |
bb69661b VZ |
177 | |
178 | if (g_isIdle) | |
3c679789 | 179 | wxapp_install_idle_handler(); |
acfd422a | 180 | |
2830bf19 RR |
181 | win->CalculateScrollbar(); |
182 | } | |
fab591c5 | 183 | #endif |
2830bf19 | 184 | |
1c35b54e VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // redraw callback for multiline text | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | #ifndef __WXGTK20__ | |
190 | ||
191 | // redrawing a GtkText from inside a wxYield() call results in crashes (the | |
192 | // text sample shows it in its "Add lines" command which shows wxProgressDialog | |
193 | // which implicitly calls wxYield()) so we override GtkText::draw() and simply | |
194 | // don't do anything if we're inside wxYield() | |
195 | ||
196 | extern bool wxIsInsideYield; | |
197 | ||
2e08b1a3 VZ |
198 | extern "C" { |
199 | typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect); | |
200 | } | |
1c35b54e VZ |
201 | |
202 | static GtkDrawCallback gs_gtk_text_draw = NULL; | |
203 | ||
204 | extern "C" | |
205 | void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect) | |
206 | { | |
207 | if ( !wxIsInsideYield ) | |
208 | { | |
209 | wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw, | |
210 | _T("infinite recursion in wxgtk_text_draw aborted") ); | |
211 | ||
212 | gs_gtk_text_draw(widget, rect); | |
213 | } | |
214 | } | |
215 | ||
216 | #endif // __WXGTK20__ | |
217 | ||
2f2aa628 RR |
218 | //----------------------------------------------------------------------------- |
219 | // wxTextCtrl | |
220 | //----------------------------------------------------------------------------- | |
221 | ||
222 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
223 | ||
c801d85f | 224 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
2830bf19 | 225 | EVT_CHAR(wxTextCtrl::OnChar) |
e702ff0f JS |
226 | |
227 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
228 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
229 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
230 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
231 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
232 | ||
233 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
234 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
235 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
236 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
237 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
c801d85f KB |
238 | END_EVENT_TABLE() |
239 | ||
01041145 | 240 | void wxTextCtrl::Init() |
f5abe911 | 241 | { |
ce2f50e3 | 242 | m_ignoreNextUpdate = |
f5abe911 | 243 | m_modified = FALSE; |
c04ec496 | 244 | SetUpdateFont(FALSE); |
3ca6a5f0 BP |
245 | m_text = |
246 | m_vScrollbar = (GtkWidget *)NULL; | |
f5abe911 | 247 | } |
13289f04 | 248 | |
13111b2a VZ |
249 | wxTextCtrl::wxTextCtrl( wxWindow *parent, |
250 | wxWindowID id, | |
251 | const wxString &value, | |
252 | const wxPoint &pos, | |
253 | const wxSize &size, | |
254 | long style, | |
255 | const wxValidator& validator, | |
256 | const wxString &name ) | |
f5abe911 | 257 | { |
01041145 VZ |
258 | Init(); |
259 | ||
f5abe911 RR |
260 | Create( parent, id, value, pos, size, style, validator, name ); |
261 | } | |
c801d85f | 262 | |
13111b2a VZ |
263 | bool wxTextCtrl::Create( wxWindow *parent, |
264 | wxWindowID id, | |
265 | const wxString &value, | |
266 | const wxPoint &pos, | |
267 | const wxSize &size, | |
268 | long style, | |
269 | const wxValidator& validator, | |
270 | const wxString &name ) | |
c801d85f | 271 | { |
2830bf19 | 272 | m_needParent = TRUE; |
b292e2f5 | 273 | m_acceptsFocus = TRUE; |
484e45bf | 274 | |
4dcaf11a RR |
275 | if (!PreCreation( parent, pos, size ) || |
276 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
277 | { | |
223d09f6 | 278 | wxFAIL_MSG( wxT("wxTextCtrl creation failed") ); |
13111b2a | 279 | return FALSE; |
4dcaf11a | 280 | } |
6de97a3b | 281 | |
805dd538 | 282 | |
034be888 | 283 | m_vScrollbarVisible = FALSE; |
13289f04 | 284 | |
2830bf19 | 285 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
a8bf1826 | 286 | |
fab591c5 RR |
287 | #ifdef __WXGTK20__ |
288 | GtkTextBuffer *buffer = NULL; | |
289 | #endif | |
290 | ||
ab46dc18 | 291 | if (multi_line) |
2830bf19 | 292 | { |
fab591c5 RR |
293 | #ifdef __WXGTK20__ |
294 | // Create view | |
295 | m_text = gtk_text_view_new(); | |
a8bf1826 | 296 | |
fab591c5 | 297 | buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
a8bf1826 | 298 | |
fab591c5 RR |
299 | // create scrolled window |
300 | m_widget = gtk_scrolled_window_new( NULL, NULL ); | |
301 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( m_widget ), | |
302 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
a8bf1826 | 303 | |
fab591c5 RR |
304 | // Insert view into scrolled window |
305 | gtk_container_add( GTK_CONTAINER(m_widget), m_text ); | |
a8bf1826 | 306 | |
fab591c5 | 307 | // Global settings which can be overridden by tags, I guess. |
9d522606 | 308 | if (HasFlag( wxHSCROLL ) || HasFlag( wxTE_DONTWRAP )) |
fab591c5 RR |
309 | gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_NONE ); |
310 | else | |
311 | gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( m_text ), GTK_WRAP_WORD ); | |
805dd538 | 312 | |
fab591c5 RR |
313 | if (!HasFlag(wxNO_BORDER)) |
314 | gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(m_widget), GTK_SHADOW_IN ); | |
055e633d RR |
315 | |
316 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
fab591c5 RR |
317 | #else |
318 | // create our control ... | |
2830bf19 RR |
319 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); |
320 | ||
fab591c5 RR |
321 | // ... and put into the upper left hand corner of the table |
322 | bool bHasHScrollbar = FALSE; | |
2830bf19 | 323 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); |
5664fc32 | 324 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); |
2830bf19 | 325 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, |
41dee9d0 | 326 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
f5368809 RR |
327 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
328 | 0, 0); | |
bb69661b | 329 | |
fab591c5 | 330 | // always wrap words |
5c387335 | 331 | gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE ); |
bb69661b | 332 | |
fab591c5 | 333 | // finally, put the vertical scrollbar in the upper right corner |
ab46dc18 RR |
334 | m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj ); |
335 | GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS ); | |
ab46dc18 RR |
336 | gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1, |
337 | GTK_FILL, | |
338 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), | |
339 | 0, 0); | |
fab591c5 | 340 | #endif |
2830bf19 RR |
341 | } |
342 | else | |
343 | { | |
fab591c5 | 344 | // a single-line text control: no need for scrollbars |
2830bf19 | 345 | m_widget = |
1c35b54e | 346 | m_text = gtk_entry_new(); |
2830bf19 | 347 | } |
484e45bf | 348 | |
db434467 | 349 | m_parent->DoAddChild( this ); |
eda40bfc | 350 | |
76fcf0f2 | 351 | m_focusWidget = m_text; |
db434467 | 352 | |
abdeb9e7 | 353 | PostCreation(size); |
484e45bf | 354 | |
2830bf19 | 355 | if (multi_line) |
2830bf19 | 356 | gtk_widget_show(m_text); |
13289f04 | 357 | |
fab591c5 | 358 | #ifndef __WXGTK20__ |
ab46dc18 RR |
359 | if (multi_line) |
360 | { | |
361 | gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed", | |
362 | (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this ); | |
1c35b54e | 363 | |
1c35b54e VZ |
364 | // only initialize gs_gtk_text_draw once, starting from the next the |
365 | // klass::draw will already be wxgtk_text_draw | |
366 | if ( !gs_gtk_text_draw ) | |
367 | { | |
368 | GtkDrawCallback& | |
369 | draw = GTK_WIDGET_CLASS(GTK_OBJECT(m_text)->klass)->draw; | |
370 | ||
371 | gs_gtk_text_draw = draw; | |
372 | ||
373 | draw = wxgtk_text_draw; | |
374 | } | |
ab46dc18 | 375 | } |
fab591c5 | 376 | #endif // GTK+ 1.x |
3358d36e | 377 | |
291a8f20 | 378 | if (!value.IsEmpty()) |
2830bf19 | 379 | { |
fab591c5 RR |
380 | #ifdef __WXGTK20__ |
381 | SetValue( value ); | |
382 | #else | |
3358d36e | 383 | |
9e691f46 | 384 | #if !GTK_CHECK_VERSION(1, 2, 0) |
3358d36e VZ |
385 | // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in |
386 | // gtk_editable_insert_text() | |
387 | gtk_widget_realize(m_text); | |
388 | #endif // GTK 1.0 | |
389 | ||
fab591c5 | 390 | gint tmp = 0; |
05939a81 | 391 | #if wxUSE_UNICODE |
3358d36e | 392 | wxWX2MBbuf val = value.mbc_str(); |
05939a81 | 393 | gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp ); |
fab591c5 | 394 | #else |
2830bf19 | 395 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); |
fab591c5 | 396 | #endif |
3358d36e | 397 | |
291a8f20 RR |
398 | if (multi_line) |
399 | { | |
fab591c5 | 400 | // Bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 401 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
3358d36e | 402 | } |
a8bf1826 | 403 | |
fab591c5 | 404 | #endif |
2830bf19 | 405 | } |
484e45bf | 406 | |
2830bf19 RR |
407 | if (style & wxTE_PASSWORD) |
408 | { | |
409 | if (!multi_line) | |
410 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
411 | } | |
8bbe427f | 412 | |
2830bf19 RR |
413 | if (style & wxTE_READONLY) |
414 | { | |
415 | if (!multi_line) | |
416 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
fab591c5 RR |
417 | #ifdef __WXGTK20__ |
418 | else | |
98a8daf4 VS |
419 | gtk_text_view_set_editable( GTK_TEXT_VIEW( m_text), FALSE); |
420 | #else | |
421 | } | |
422 | else | |
423 | { | |
424 | if (multi_line) | |
425 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
426 | #endif | |
427 | } | |
428 | ||
c663fbea VS |
429 | #ifdef __WXGTK20__ |
430 | if (multi_line) | |
431 | { | |
432 | if (style & wxTE_RIGHT) | |
433 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_RIGHT ); | |
434 | else if (style & wxTE_CENTRE) | |
435 | gtk_text_view_set_justification( GTK_TEXT_VIEW(m_text), GTK_JUSTIFY_CENTER ); | |
436 | // Left justify (alignment) is the default and we don't need to apply GTK_JUSTIFY_LEFT | |
437 | } | |
438 | // gtk_entry_set_alignment was introduced in gtk+-2.3.5 | |
439 | #if GTK_CHECK_VERSION(2, 3, 5) | |
440 | else | |
441 | { | |
442 | if (style & wxTE_RIGHT) | |
443 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 1.0 ); | |
444 | else if (style & wxTE_CENTRE) | |
445 | gtk_entry_set_alignment( GTK_ENTRY(m_text), 0.5 ); | |
446 | } | |
447 | #endif // gtk+-2.3.5 | |
448 | #endif // __WXGTK20__ | |
9d522606 | 449 | |
fab591c5 RR |
450 | // We want to be notified about text changes. |
451 | #ifdef __WXGTK20__ | |
452 | if (multi_line) | |
453 | { | |
454 | g_signal_connect( G_OBJECT(buffer), "changed", | |
455 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
456 | } | |
457 | else | |
458 | #endif | |
9d522606 | 459 | |
fab591c5 | 460 | { |
055e633d RR |
461 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
462 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
fab591c5 | 463 | } |
ce16e5d7 | 464 | |
65045edd | 465 | m_cursor = wxCursor( wxCURSOR_IBEAM ); |
13111b2a | 466 | |
9d522606 | 467 | wxTextAttr attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont()); |
17665a2b VZ |
468 | SetDefaultStyle( attrDef ); |
469 | ||
2830bf19 RR |
470 | return TRUE; |
471 | } | |
484e45bf | 472 | |
9d522606 | 473 | |
2830bf19 RR |
474 | void wxTextCtrl::CalculateScrollbar() |
475 | { | |
fab591c5 | 476 | #ifndef __WXGTK20__ |
2830bf19 | 477 | if ((m_windowStyle & wxTE_MULTILINE) == 0) return; |
f96aa4d9 | 478 | |
2830bf19 | 479 | GtkAdjustment *adj = GTK_TEXT(m_text)->vadj; |
805dd538 | 480 | |
2830bf19 RR |
481 | if (adj->upper - adj->page_size < 0.8) |
482 | { | |
483 | if (m_vScrollbarVisible) | |
484 | { | |
034be888 | 485 | gtk_widget_hide( m_vScrollbar ); |
034be888 | 486 | m_vScrollbarVisible = FALSE; |
2830bf19 RR |
487 | } |
488 | } | |
489 | else | |
490 | { | |
491 | if (!m_vScrollbarVisible) | |
492 | { | |
034be888 | 493 | gtk_widget_show( m_vScrollbar ); |
034be888 | 494 | m_vScrollbarVisible = TRUE; |
2830bf19 RR |
495 | } |
496 | } | |
fab591c5 | 497 | #endif |
6de97a3b | 498 | } |
c801d85f | 499 | |
03f38c58 | 500 | wxString wxTextCtrl::GetValue() const |
c801d85f | 501 | { |
223d09f6 | 502 | wxCHECK_MSG( m_text != NULL, wxT(""), wxT("invalid text ctrl") ); |
8bbe427f | 503 | |
2830bf19 RR |
504 | wxString tmp; |
505 | if (m_windowStyle & wxTE_MULTILINE) | |
506 | { | |
fab591c5 RR |
507 | #ifdef __WXGTK20__ |
508 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
a8bf1826 | 509 | |
fab591c5 RR |
510 | GtkTextIter start; |
511 | gtk_text_buffer_get_start_iter( text_buffer, &start ); | |
512 | GtkTextIter end; | |
513 | gtk_text_buffer_get_end_iter( text_buffer, &end ); | |
514 | gchar *text = gtk_text_buffer_get_text( text_buffer, &start, &end, TRUE ); | |
5b87f8bf | 515 | |
fab591c5 RR |
516 | #if wxUSE_UNICODE |
517 | wxWCharBuffer buffer( wxConvUTF8.cMB2WX( text ) ); | |
518 | #else | |
519 | wxCharBuffer buffer( wxConvLocal.cWC2WX( wxConvUTF8.cMB2WC( text ) ) ); | |
520 | #endif | |
521 | tmp = buffer; | |
a8bf1826 | 522 | |
fab591c5 RR |
523 | g_free( text ); |
524 | #else | |
2830bf19 RR |
525 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
526 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
fab591c5 | 527 | tmp = text; |
2830bf19 | 528 | g_free( text ); |
fab591c5 | 529 | #endif |
2830bf19 RR |
530 | } |
531 | else | |
532 | { | |
fab591c5 | 533 | tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) ); |
2830bf19 | 534 | } |
a8bf1826 | 535 | |
2830bf19 | 536 | return tmp; |
6de97a3b | 537 | } |
c801d85f KB |
538 | |
539 | void wxTextCtrl::SetValue( const wxString &value ) | |
540 | { | |
223d09f6 | 541 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 542 | |
2830bf19 RR |
543 | if (m_windowStyle & wxTE_MULTILINE) |
544 | { | |
fab591c5 RR |
545 | #ifdef __WXGTK20__ |
546 | ||
547 | #if wxUSE_UNICODE | |
548 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) ); | |
549 | #else | |
550 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) ); | |
a8bf1826 | 551 | #endif |
fab591c5 RR |
552 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
553 | gtk_text_buffer_set_text( text_buffer, buffer, strlen(buffer) ); | |
a8bf1826 | 554 | |
fab591c5 | 555 | #else |
2830bf19 RR |
556 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
557 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
558 | len = 0; | |
01041145 | 559 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len ); |
05939a81 | 560 | #endif |
2830bf19 RR |
561 | } |
562 | else | |
563 | { | |
fab591c5 | 564 | gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) ); |
2830bf19 | 565 | } |
f6bcfd97 BP |
566 | |
567 | // GRG, Jun/2000: Changed this after a lot of discussion in | |
77ffb593 | 568 | // the lists. wxWidgets 2.2 will have a set of flags to |
f6bcfd97 BP |
569 | // customize this behaviour. |
570 | SetInsertionPoint(0); | |
a8bf1826 | 571 | |
f6bcfd97 | 572 | m_modified = FALSE; |
6de97a3b | 573 | } |
c801d85f KB |
574 | |
575 | void wxTextCtrl::WriteText( const wxString &text ) | |
576 | { | |
223d09f6 | 577 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 578 | |
17665a2b VZ |
579 | if ( text.empty() ) |
580 | return; | |
484e45bf | 581 | |
c04ec496 VZ |
582 | // gtk_text_changed_callback() will set m_modified to true but m_modified |
583 | // shouldn't be changed by the program writing to the text control itself, | |
584 | // so save the old value and restore when we're done | |
585 | bool oldModified = m_modified; | |
586 | ||
17665a2b | 587 | if ( m_windowStyle & wxTE_MULTILINE ) |
2830bf19 | 588 | { |
fab591c5 RR |
589 | #ifdef __WXGTK20__ |
590 | ||
591 | #if wxUSE_UNICODE | |
592 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) ); | |
593 | #else | |
594 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) ); | |
a8bf1826 | 595 | #endif |
fab591c5 | 596 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
5b87f8bf | 597 | |
e136b747 | 598 | // TODO: Call whatever is needed to delete the selection. |
cc3da3f8 | 599 | wxGtkTextInsert( m_text, text_buffer, m_defaultStyle, buffer ); |
a8bf1826 | 600 | |
71aba833 VZ |
601 | GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) ); |
602 | // Scroll to cursor, but only if scrollbar thumb is at the very bottom | |
603 | if ( adj->value == adj->upper - adj->page_size ) | |
604 | { | |
605 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), | |
606 | gtk_text_buffer_get_insert( text_buffer ), 0.0, FALSE, 0.0, 1.0 ); | |
607 | } | |
a8bf1826 | 608 | #else // GTK 1.x |
ba3f6b44 | 609 | // After cursor movements, gtk_text_get_point() is wrong by one. |
9e691f46 | 610 | gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) ); |
eda40bfc | 611 | |
41b2e0e6 VZ |
612 | // always use m_defaultStyle, even if it is empty as otherwise |
613 | // resetting the style and appending some more text wouldn't work: if | |
614 | // we don't specify the style explicitly, the old style would be used | |
2b5f62a0 | 615 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); |
fab591c5 | 616 | wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len()); |
eda40bfc | 617 | |
07e9d4f0 VZ |
618 | // we called wxGtkTextInsert with correct font, no need to do anything |
619 | // in UpdateFontIfNeeded() any longer | |
620 | if ( !text.empty() ) | |
621 | { | |
c04ec496 | 622 | SetUpdateFont(FALSE); |
07e9d4f0 VZ |
623 | } |
624 | ||
ba3f6b44 | 625 | // Bring editable's cursor back uptodate. |
9e691f46 | 626 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
a8bf1826 | 627 | #endif // GTK 1.x/2.0 |
2830bf19 | 628 | } |
17665a2b | 629 | else // single line |
2830bf19 | 630 | { |
2b5f62a0 VZ |
631 | // First remove the selection if there is one |
632 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); | |
633 | ||
ba3f6b44 | 634 | // This moves the cursor pos to behind the inserted text. |
9e691f46 | 635 | gint len = GET_EDITABLE_POS(m_text); |
a8bf1826 | 636 | |
fab591c5 RR |
637 | #ifdef __WXGTK20__ |
638 | ||
639 | #if wxUSE_UNICODE | |
640 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) ); | |
641 | #else | |
642 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) ); | |
a8bf1826 | 643 | #endif |
fab591c5 | 644 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len ); |
a8bf1826 | 645 | |
fab591c5 RR |
646 | #else |
647 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len ); | |
648 | #endif | |
3358d36e | 649 | |
ba3f6b44 | 650 | // Bring entry's cursor uptodate. |
9e691f46 | 651 | gtk_entry_set_position( GTK_ENTRY(m_text), len ); |
2830bf19 | 652 | } |
eda40bfc | 653 | |
c04ec496 | 654 | m_modified = oldModified; |
6de97a3b | 655 | } |
c801d85f | 656 | |
a6e21573 HH |
657 | void wxTextCtrl::AppendText( const wxString &text ) |
658 | { | |
ba3f6b44 RR |
659 | SetInsertionPointEnd(); |
660 | WriteText( text ); | |
661 | } | |
2df7be7f | 662 | |
ba3f6b44 RR |
663 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
664 | { | |
a6e21573 HH |
665 | if (m_windowStyle & wxTE_MULTILINE) |
666 | { | |
fab591c5 | 667 | #ifndef __WXGTK20__ |
ba3f6b44 RR |
668 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
669 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
bb69661b | 670 | |
ba3f6b44 RR |
671 | if (text) |
672 | { | |
673 | wxString buf(wxT("")); | |
674 | long i; | |
675 | int currentLine = 0; | |
676 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
677 | if (text[i] == '\n') | |
678 | currentLine++; | |
679 | // Now get the text | |
680 | int j; | |
681 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
682 | buf += text[i]; | |
17665a2b | 683 | |
ba3f6b44 RR |
684 | g_free( text ); |
685 | return buf; | |
bb69661b | 686 | } |
ba3f6b44 | 687 | else |
bb69661b | 688 | { |
ba3f6b44 | 689 | return wxEmptyString; |
bb69661b | 690 | } |
905f2110 JS |
691 | #else |
692 | GtkTextBuffer *text_buffer; | |
693 | text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(m_text)); | |
694 | GtkTextIter line; | |
695 | gtk_text_buffer_get_iter_at_line(text_buffer,&line,lineNo); | |
696 | GtkTextIter end; | |
697 | gtk_text_buffer_get_end_iter(text_buffer,&end ); | |
698 | gchar *text = gtk_text_buffer_get_text(text_buffer,&line,&end,TRUE); | |
58192970 | 699 | wxString result(wxGTK_CONV_BACK(text)); |
905f2110 JS |
700 | g_free(text); |
701 | return result.BeforeFirst(wxT('\n')); | |
702 | #endif | |
a6e21573 | 703 | } |
ba3f6b44 | 704 | else |
a81258be | 705 | { |
ba3f6b44 RR |
706 | if (lineNo == 0) return GetValue(); |
707 | return wxEmptyString; | |
a81258be | 708 | } |
6de97a3b | 709 | } |
c801d85f | 710 | |
a81258be RR |
711 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
712 | { | |
ac0d36b5 HH |
713 | /* If you implement this, don't forget to update the documentation! |
714 | * (file docs/latex/wx/text.tex) */ | |
223d09f6 | 715 | wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") ); |
a81258be | 716 | } |
112892b9 | 717 | |
0efe5ba7 | 718 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 719 | { |
96385642 | 720 | if ( m_windowStyle & wxTE_MULTILINE ) |
805dd538 | 721 | { |
96385642 VZ |
722 | wxString text = GetValue(); |
723 | ||
6085b116 | 724 | // cast to prevent warning. But pos really should've been unsigned. |
2829d9e3 | 725 | if( (unsigned long)pos > text.Len() ) |
96385642 VZ |
726 | return FALSE; |
727 | ||
ac0d36b5 HH |
728 | *x=0; // First Col |
729 | *y=0; // First Line | |
2829d9e3 | 730 | |
05939a81 OK |
731 | const wxChar* stop = text.c_str() + pos; |
732 | for ( const wxChar *p = text.c_str(); p < stop; p++ ) | |
96385642 | 733 | { |
223d09f6 | 734 | if (*p == wxT('\n')) |
96385642 VZ |
735 | { |
736 | (*y)++; | |
ac0d36b5 | 737 | *x=0; |
96385642 VZ |
738 | } |
739 | else | |
740 | (*x)++; | |
741 | } | |
805dd538 | 742 | } |
96385642 VZ |
743 | else // single line control |
744 | { | |
2829d9e3 | 745 | if ( pos <= GTK_ENTRY(m_text)->text_length ) |
96385642 | 746 | { |
ac0d36b5 | 747 | *y = 0; |
96385642 VZ |
748 | *x = pos; |
749 | } | |
750 | else | |
751 | { | |
752 | // index out of bounds | |
753 | return FALSE; | |
754 | } | |
8bbe427f | 755 | } |
96385642 VZ |
756 | |
757 | return TRUE; | |
6de97a3b | 758 | } |
c801d85f | 759 | |
e3ca08dd | 760 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 761 | { |
2830bf19 | 762 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; |
805dd538 | 763 | |
2830bf19 | 764 | long pos=0; |
ac0d36b5 | 765 | for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n' |
8bbe427f | 766 | |
3358d36e | 767 | pos += x; |
2830bf19 | 768 | return pos; |
6de97a3b | 769 | } |
c801d85f | 770 | |
a81258be | 771 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 772 | { |
a81258be RR |
773 | wxString str = GetLineText (lineNo); |
774 | return (int) str.Length(); | |
6de97a3b | 775 | } |
c801d85f | 776 | |
a81258be | 777 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 778 | { |
2830bf19 | 779 | if (m_windowStyle & wxTE_MULTILINE) |
a81258be | 780 | { |
fab591c5 RR |
781 | #ifdef __WXGTK20__ |
782 | GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
a8bf1826 | 783 | |
fab591c5 RR |
784 | return gtk_text_buffer_get_line_count( buffer ); |
785 | #else | |
2830bf19 RR |
786 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
787 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
788 | ||
789 | if (text) | |
790 | { | |
791 | int currentLine = 0; | |
792 | for (int i = 0; i < len; i++ ) | |
96385642 | 793 | { |
2830bf19 | 794 | if (text[i] == '\n') |
96385642 VZ |
795 | currentLine++; |
796 | } | |
2830bf19 | 797 | g_free( text ); |
2829d9e3 VZ |
798 | |
799 | // currentLine is 0 based, add 1 to get number of lines | |
800 | return currentLine + 1; | |
2830bf19 RR |
801 | } |
802 | else | |
96385642 | 803 | { |
2830bf19 | 804 | return 0; |
96385642 | 805 | } |
fab591c5 | 806 | #endif |
a81258be RR |
807 | } |
808 | else | |
2830bf19 | 809 | { |
96385642 | 810 | return 1; |
2830bf19 | 811 | } |
6de97a3b | 812 | } |
c801d85f | 813 | |
debe6624 | 814 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 815 | { |
223d09f6 | 816 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3358d36e VZ |
817 | |
818 | if (m_windowStyle & wxTE_MULTILINE) | |
291a8f20 | 819 | { |
fab591c5 RR |
820 | #ifdef __WXGTK20__ |
821 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
822 | GtkTextIter iter; | |
823 | gtk_text_buffer_get_iter_at_offset( text_buffer, &iter, pos ); | |
824 | gtk_text_buffer_place_cursor( text_buffer, &iter ); | |
825 | #else | |
291a8f20 RR |
826 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_text), |
827 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e | 828 | |
291a8f20 | 829 | /* we fake a set_point by inserting and deleting. as the user |
fab591c5 | 830 | isn't supposed to get to know about this non-sense, we |
3358d36e VZ |
831 | disconnect so that no events are sent to the user program. */ |
832 | ||
291a8f20 RR |
833 | gint tmp = (gint)pos; |
834 | gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp ); | |
3358d36e VZ |
835 | gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp ); |
836 | ||
291a8f20 RR |
837 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
838 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e | 839 | |
fab591c5 | 840 | // bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 841 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
fab591c5 | 842 | #endif |
ac0d36b5 | 843 | } |
2830bf19 | 844 | else |
291a8f20 | 845 | { |
d59051dd | 846 | gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos ); |
3358d36e | 847 | |
fab591c5 | 848 | // Bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 849 | SET_EDITABLE_POS(m_text, (guint32)pos); |
291a8f20 | 850 | } |
6de97a3b | 851 | } |
c801d85f | 852 | |
03f38c58 | 853 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 854 | { |
223d09f6 | 855 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 856 | |
d59051dd | 857 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
858 | { |
859 | #ifdef __WXGTK20__ | |
860 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
861 | GtkTextIter end; | |
862 | gtk_text_buffer_get_end_iter( text_buffer, &end ); | |
863 | gtk_text_buffer_place_cursor( text_buffer, &end ); | |
864 | #else | |
d59051dd | 865 | SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text))); |
fab591c5 RR |
866 | #endif |
867 | } | |
d59051dd | 868 | else |
fab591c5 | 869 | { |
d59051dd | 870 | gtk_entry_set_position( GTK_ENTRY(m_text), -1 ); |
fab591c5 | 871 | } |
6de97a3b | 872 | } |
c801d85f | 873 | |
debe6624 | 874 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 875 | { |
223d09f6 | 876 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 877 | |
2830bf19 | 878 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
879 | { |
880 | #ifdef __WXGTK20__ | |
881 | gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable ); | |
882 | #else | |
2830bf19 | 883 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); |
fab591c5 RR |
884 | #endif |
885 | } | |
2830bf19 | 886 | else |
fab591c5 | 887 | { |
2830bf19 | 888 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); |
fab591c5 | 889 | } |
6de97a3b | 890 | } |
c801d85f | 891 | |
68df5777 RR |
892 | bool wxTextCtrl::Enable( bool enable ) |
893 | { | |
894 | if (!wxWindowBase::Enable(enable)) | |
895 | { | |
896 | // nothing to do | |
897 | return FALSE; | |
898 | } | |
f6bcfd97 | 899 | |
68df5777 RR |
900 | if (m_windowStyle & wxTE_MULTILINE) |
901 | { | |
fab591c5 RR |
902 | #ifdef __WXGTK20__ |
903 | SetEditable( enable ); | |
904 | #else | |
68df5777 | 905 | gtk_text_set_editable( GTK_TEXT(m_text), enable ); |
5abf8c9d | 906 | OnParentEnable(enable); |
fab591c5 | 907 | #endif |
68df5777 RR |
908 | } |
909 | else | |
910 | { | |
911 | gtk_widget_set_sensitive( m_text, enable ); | |
912 | } | |
913 | ||
914 | return TRUE; | |
915 | } | |
916 | ||
fdca68a6 JS |
917 | // wxGTK-specific: called recursively by Enable, |
918 | // to give widgets an oppprtunity to correct their colours after they | |
919 | // have been changed by Enable | |
920 | void wxTextCtrl::OnParentEnable( bool enable ) | |
921 | { | |
922 | // If we have a custom background colour, we use this colour in both | |
923 | // disabled and enabled mode, or we end up with a different colour under the | |
924 | // text. | |
925 | wxColour oldColour = GetBackgroundColour(); | |
926 | if (oldColour.Ok()) | |
927 | { | |
928 | // Need to set twice or it'll optimize the useful stuff out | |
929 | if (oldColour == * wxWHITE) | |
930 | SetBackgroundColour(*wxBLACK); | |
931 | else | |
932 | SetBackgroundColour(*wxWHITE); | |
933 | SetBackgroundColour(oldColour); | |
934 | } | |
935 | } | |
936 | ||
3a9fa0d6 VZ |
937 | void wxTextCtrl::MarkDirty() |
938 | { | |
939 | m_modified = TRUE; | |
940 | } | |
941 | ||
0efe5ba7 VZ |
942 | void wxTextCtrl::DiscardEdits() |
943 | { | |
944 | m_modified = FALSE; | |
945 | } | |
946 | ||
ce2f50e3 VZ |
947 | // ---------------------------------------------------------------------------- |
948 | // max text length support | |
949 | // ---------------------------------------------------------------------------- | |
950 | ||
951 | void wxTextCtrl::IgnoreNextTextUpdate() | |
952 | { | |
953 | m_ignoreNextUpdate = TRUE; | |
954 | } | |
955 | ||
956 | bool wxTextCtrl::IgnoreTextUpdate() | |
957 | { | |
958 | if ( m_ignoreNextUpdate ) | |
959 | { | |
960 | m_ignoreNextUpdate = FALSE; | |
961 | ||
962 | return TRUE; | |
963 | } | |
964 | ||
965 | return FALSE; | |
966 | } | |
967 | ||
d7eee191 VZ |
968 | void wxTextCtrl::SetMaxLength(unsigned long len) |
969 | { | |
970 | if ( !HasFlag(wxTE_MULTILINE) ) | |
971 | { | |
972 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
ce2f50e3 VZ |
973 | |
974 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
975 | // we had tried to enter more text than allowed by max text length and | |
976 | // the text wasn't really changed | |
977 | // | |
978 | // to detect this and generate TEXT_MAXLEN event instead of | |
979 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
980 | // | |
981 | // when max len is set to 0 we disconnect our handler as it means that | |
982 | // we shouldn't check anything any more | |
983 | if ( len ) | |
984 | { | |
985 | gtk_signal_connect( GTK_OBJECT(m_text), | |
986 | "insert_text", | |
987 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
988 | (gpointer)this); | |
989 | } | |
990 | else // no checking | |
991 | { | |
992 | gtk_signal_disconnect_by_func | |
993 | ( | |
994 | GTK_OBJECT(m_text), | |
995 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
996 | (gpointer)this | |
997 | ); | |
998 | } | |
d7eee191 VZ |
999 | } |
1000 | } | |
1001 | ||
debe6624 | 1002 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 1003 | { |
223d09f6 | 1004 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1005 | |
2b5f62a0 VZ |
1006 | if (from == -1 && to == -1) |
1007 | { | |
1008 | from = 0; | |
1009 | to = GetValue().Length(); | |
1010 | } | |
1011 | ||
fab591c5 | 1012 | #ifndef __WXGTK20__ |
a1f79c1e VZ |
1013 | if ( (m_windowStyle & wxTE_MULTILINE) && |
1014 | !GTK_TEXT(m_text)->line_start_cache ) | |
1015 | { | |
1016 | // tell the programmer that it didn't work | |
1017 | wxLogDebug(_T("Can't call SetSelection() before realizing the control")); | |
1018 | return; | |
1019 | } | |
fab591c5 | 1020 | #endif |
a1f79c1e | 1021 | |
fab591c5 RR |
1022 | if (m_windowStyle & wxTE_MULTILINE) |
1023 | { | |
1024 | #ifdef __WXGTK20__ | |
739e366a JS |
1025 | GtkTextBuffer *buf = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
1026 | ||
1027 | GtkTextIter fromi, toi; | |
1028 | gtk_text_buffer_get_iter_at_offset( buf, &fromi, from ); | |
1029 | gtk_text_buffer_get_iter_at_offset( buf, &toi, to ); | |
1030 | ||
1031 | gtk_text_buffer_place_cursor( buf, &toi ); | |
1032 | gtk_text_buffer_move_mark_by_name( buf, "selection_bound", &fromi ); | |
fab591c5 RR |
1033 | #else |
1034 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1035 | #endif | |
1036 | } | |
1037 | else | |
1038 | { | |
1039 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1040 | } | |
6de97a3b | 1041 | } |
c801d85f | 1042 | |
afbe906a | 1043 | void wxTextCtrl::ShowPosition( long pos ) |
c801d85f | 1044 | { |
afbe906a RR |
1045 | if (m_windowStyle & wxTE_MULTILINE) |
1046 | { | |
71aba833 VZ |
1047 | #ifdef __WXGTK20__ |
1048 | GtkTextBuffer *buf = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1049 | GtkTextIter iter; | |
1050 | gtk_text_buffer_get_start_iter( buf, &iter ); | |
1051 | gtk_text_iter_set_offset( &iter, pos ); | |
1052 | GtkTextMark *mark = gtk_text_buffer_create_mark( buf, NULL, &iter, TRUE ); | |
1053 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 ); | |
1054 | #else // GTK 1.x | |
afbe906a RR |
1055 | GtkAdjustment *vp = GTK_TEXT(m_text)->vadj; |
1056 | float totalLines = (float) GetNumberOfLines(); | |
1057 | long posX; | |
1058 | long posY; | |
1059 | PositionToXY(pos, &posX, &posY); | |
1060 | float posLine = (float) posY; | |
1061 | float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower; | |
1062 | gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p); | |
71aba833 | 1063 | #endif // GTK 1.x/2.x |
afbe906a | 1064 | } |
6de97a3b | 1065 | } |
c801d85f | 1066 | |
692c9b86 VZ |
1067 | #ifdef __WXGTK20__ |
1068 | ||
1069 | wxTextCtrlHitTestResult | |
1070 | wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const | |
1071 | { | |
1072 | if ( !IsMultiLine() ) | |
1073 | { | |
1074 | // not supported | |
1075 | return wxTE_HT_UNKNOWN; | |
1076 | } | |
1077 | ||
1078 | int x, y; | |
1079 | gtk_text_view_window_to_buffer_coords | |
1080 | ( | |
1081 | GTK_TEXT_VIEW(m_text), | |
1082 | GTK_TEXT_WINDOW_TEXT, | |
1083 | pt.x, pt.y, | |
1084 | &x, &y | |
1085 | ); | |
1086 | ||
1087 | GtkTextIter iter; | |
1088 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); | |
1089 | if ( pos ) | |
1090 | *pos = gtk_text_iter_get_offset(&iter); | |
1091 | ||
1092 | return wxTE_HT_ON_TEXT; | |
1093 | } | |
1094 | ||
1095 | #endif // __WXGTK20__ | |
1096 | ||
03f38c58 | 1097 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 1098 | { |
223d09f6 | 1099 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1100 | |
fab591c5 RR |
1101 | #ifdef __WXGTK20__ |
1102 | if (m_windowStyle & wxTE_MULTILINE) | |
1103 | { | |
1104 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
a8bf1826 | 1105 | |
fab591c5 RR |
1106 | // There is no direct accessor for the cursor, but |
1107 | // internally, the cursor is the "mark" called | |
1108 | // "insert" in the text view's btree structure. | |
a8bf1826 | 1109 | |
fab591c5 RR |
1110 | GtkTextMark *mark = gtk_text_buffer_get_insert( text_buffer ); |
1111 | GtkTextIter cursor; | |
1112 | gtk_text_buffer_get_iter_at_mark( text_buffer, &cursor, mark ); | |
a8bf1826 | 1113 | |
fab591c5 RR |
1114 | return gtk_text_iter_get_offset( &cursor ); |
1115 | } | |
1116 | else | |
1117 | #endif | |
1118 | { | |
9e691f46 | 1119 | return (long) GET_EDITABLE_POS(m_text); |
fab591c5 | 1120 | } |
6de97a3b | 1121 | } |
c801d85f | 1122 | |
03f38c58 | 1123 | long wxTextCtrl::GetLastPosition() const |
c801d85f | 1124 | { |
223d09f6 | 1125 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1126 | |
2830bf19 | 1127 | int pos = 0; |
a8bf1826 | 1128 | |
2830bf19 | 1129 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1130 | { |
1131 | #ifdef __WXGTK20__ | |
1132 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1133 | GtkTextIter end; | |
1134 | gtk_text_buffer_get_end_iter( text_buffer, &end ); | |
a8bf1826 | 1135 | |
fab591c5 RR |
1136 | pos = gtk_text_iter_get_offset( &end ); |
1137 | #else | |
2830bf19 | 1138 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
fab591c5 RR |
1139 | #endif |
1140 | } | |
2830bf19 | 1141 | else |
fab591c5 | 1142 | { |
2830bf19 | 1143 | pos = GTK_ENTRY(m_text)->text_length; |
fab591c5 | 1144 | } |
805dd538 | 1145 | |
ac0d36b5 | 1146 | return (long)pos; |
6de97a3b | 1147 | } |
c801d85f | 1148 | |
debe6624 | 1149 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 1150 | { |
223d09f6 | 1151 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1152 | |
581ee8a9 | 1153 | #ifdef __WXGTK20__ |
fdd55287 | 1154 | if (m_windowStyle & wxTE_MULTILINE) |
581ee8a9 VZ |
1155 | { |
1156 | GtkTextBuffer * | |
1157 | text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1158 | ||
1159 | GtkTextIter fromi, toi; | |
1160 | gtk_text_buffer_get_iter_at_offset( text_buffer, &fromi, from ); | |
1161 | gtk_text_buffer_get_iter_at_offset( text_buffer, &toi, to ); | |
1162 | ||
1163 | gtk_text_buffer_delete( text_buffer, &fromi, &toi ); | |
1164 | } | |
1165 | else // single line | |
fab591c5 | 1166 | #endif |
581ee8a9 | 1167 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 1168 | } |
c801d85f | 1169 | |
debe6624 | 1170 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 1171 | { |
223d09f6 | 1172 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1173 | |
581ee8a9 | 1174 | Remove( from, to ); |
bb69661b | 1175 | |
2df7be7f RR |
1176 | if (!value.IsEmpty()) |
1177 | { | |
581ee8a9 VZ |
1178 | #ifdef __WXGTK20__ |
1179 | SetInsertionPoint( from ); | |
1180 | WriteText( value ); | |
1181 | #else // GTK 1.x | |
2df7be7f | 1182 | gint pos = (gint)from; |
05939a81 | 1183 | #if wxUSE_UNICODE |
2df7be7f RR |
1184 | wxWX2MBbuf buf = value.mbc_str(); |
1185 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos ); | |
05939a81 | 1186 | #else |
2df7be7f | 1187 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
581ee8a9 VZ |
1188 | #endif // wxUSE_UNICODE |
1189 | #endif // GTK 1.x/2.x | |
2df7be7f | 1190 | } |
6de97a3b | 1191 | } |
c801d85f | 1192 | |
03f38c58 | 1193 | void wxTextCtrl::Cut() |
c801d85f | 1194 | { |
223d09f6 | 1195 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1196 | |
bbde2e29 VS |
1197 | #ifdef __WXGTK20__ |
1198 | if (m_windowStyle & wxTE_MULTILINE) | |
1199 | g_signal_emit_by_name(m_text, "cut-clipboard"); | |
1200 | else | |
fab591c5 | 1201 | #endif |
bbde2e29 | 1202 | gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1203 | } |
c801d85f | 1204 | |
03f38c58 | 1205 | void wxTextCtrl::Copy() |
c801d85f | 1206 | { |
223d09f6 | 1207 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1208 | |
bbde2e29 VS |
1209 | #ifdef __WXGTK20__ |
1210 | if (m_windowStyle & wxTE_MULTILINE) | |
1211 | g_signal_emit_by_name(m_text, "copy-clipboard"); | |
1212 | else | |
fab591c5 | 1213 | #endif |
bbde2e29 | 1214 | gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1215 | } |
c801d85f | 1216 | |
03f38c58 | 1217 | void wxTextCtrl::Paste() |
c801d85f | 1218 | { |
223d09f6 | 1219 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1220 | |
bbde2e29 VS |
1221 | #ifdef __WXGTK20__ |
1222 | if (m_windowStyle & wxTE_MULTILINE) | |
1223 | g_signal_emit_by_name(m_text, "paste-clipboard"); | |
1224 | else | |
fab591c5 | 1225 | #endif |
bbde2e29 | 1226 | gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1227 | } |
c801d85f | 1228 | |
ca8b28f2 JS |
1229 | // Undo/redo |
1230 | void wxTextCtrl::Undo() | |
1231 | { | |
1232 | // TODO | |
223d09f6 | 1233 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); |
ca8b28f2 JS |
1234 | } |
1235 | ||
1236 | void wxTextCtrl::Redo() | |
1237 | { | |
1238 | // TODO | |
223d09f6 | 1239 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); |
ca8b28f2 JS |
1240 | } |
1241 | ||
1242 | bool wxTextCtrl::CanUndo() const | |
1243 | { | |
1244 | // TODO | |
4855a477 | 1245 | //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); |
ca8b28f2 JS |
1246 | return FALSE; |
1247 | } | |
1248 | ||
1249 | bool wxTextCtrl::CanRedo() const | |
1250 | { | |
1251 | // TODO | |
4855a477 | 1252 | //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); |
ca8b28f2 JS |
1253 | return FALSE; |
1254 | } | |
1255 | ||
1256 | // If the return values from and to are the same, there is no | |
1257 | // selection. | |
2d4cc5b6 | 1258 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const |
ca8b28f2 | 1259 | { |
223d09f6 | 1260 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
bb69661b | 1261 | |
5b87f8bf RD |
1262 | gint from = -1; |
1263 | gint to = -1; | |
1264 | bool haveSelection = FALSE; | |
1265 | ||
fb47b99f | 1266 | #ifdef __WXGTK20__ |
5b87f8bf RD |
1267 | if (m_windowStyle & wxTE_MULTILINE) |
1268 | { | |
1269 | GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (m_text)); | |
1270 | GtkTextIter ifrom, ito; | |
1271 | if ( gtk_text_buffer_get_selection_bounds(buffer, &ifrom, &ito) ) | |
1272 | { | |
1273 | haveSelection = TRUE; | |
1274 | from = gtk_text_iter_get_offset(&ifrom); | |
1275 | to = gtk_text_iter_get_offset(&ito); | |
1276 | } | |
1277 | } | |
1278 | else // not multi-line | |
1279 | { | |
1280 | if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text), | |
1281 | &from, &to) ) | |
1282 | { | |
1283 | haveSelection = TRUE; | |
1284 | } | |
1285 | } | |
1286 | #else // not GTK2 | |
1287 | if ( (GTK_EDITABLE(m_text)->has_selection) ) | |
1288 | { | |
1289 | haveSelection = TRUE; | |
1290 | from = (long) GTK_EDITABLE(m_text)->selection_start_pos; | |
1291 | to = (long) GTK_EDITABLE(m_text)->selection_end_pos; | |
1292 | } | |
9e691f46 | 1293 | #endif |
2d4cc5b6 | 1294 | |
5b87f8bf RD |
1295 | if (! haveSelection ) |
1296 | from = to = GetInsertionPoint(); | |
1297 | ||
1298 | if ( from > to ) | |
1299 | { | |
1300 | // exchange them to be compatible with wxMSW | |
1301 | gint tmp = from; | |
1302 | from = to; | |
1303 | to = tmp; | |
1304 | } | |
bb69661b | 1305 | |
2d4cc5b6 VZ |
1306 | if ( fromOut ) |
1307 | *fromOut = from; | |
1308 | if ( toOut ) | |
1309 | *toOut = to; | |
ca8b28f2 JS |
1310 | } |
1311 | ||
5b87f8bf | 1312 | |
ca8b28f2 JS |
1313 | bool wxTextCtrl::IsEditable() const |
1314 | { | |
223d09f6 | 1315 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); |
05060eeb | 1316 | |
9e691f46 | 1317 | #ifdef __WXGTK20__ |
fdd55287 VZ |
1318 | if (m_windowStyle & wxTE_MULTILINE) |
1319 | { | |
1320 | return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text)); | |
1321 | } | |
1322 | else | |
1323 | { | |
1324 | return gtk_editable_get_editable(GTK_EDITABLE(m_text)); | |
1325 | } | |
9e691f46 | 1326 | #else |
05060eeb | 1327 | return GTK_EDITABLE(m_text)->editable; |
9e691f46 | 1328 | #endif |
ca8b28f2 JS |
1329 | } |
1330 | ||
0efe5ba7 VZ |
1331 | bool wxTextCtrl::IsModified() const |
1332 | { | |
1333 | return m_modified; | |
1334 | } | |
1335 | ||
03f38c58 | 1336 | void wxTextCtrl::Clear() |
c801d85f | 1337 | { |
223d09f6 | 1338 | SetValue( wxT("") ); |
6de97a3b | 1339 | } |
c801d85f | 1340 | |
903f689b | 1341 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 1342 | { |
223d09f6 | 1343 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
805dd538 | 1344 | |
12a3f227 | 1345 | if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
2830bf19 RR |
1346 | { |
1347 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1348 | event.SetEventObject(this); | |
f6bcfd97 | 1349 | event.SetString(GetValue()); |
2830bf19 RR |
1350 | if (GetEventHandler()->ProcessEvent(event)) return; |
1351 | } | |
903f689b | 1352 | |
12a3f227 | 1353 | if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE)) |
da048e3d | 1354 | { |
2b328fc9 RR |
1355 | // This will invoke the dialog default action, such |
1356 | // as the clicking the default button. | |
a8bf1826 | 1357 | |
da048e3d | 1358 | wxWindow *top_frame = m_parent; |
8487f887 | 1359 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
da048e3d | 1360 | top_frame = top_frame->GetParent(); |
a8bf1826 | 1361 | |
2b328fc9 | 1362 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) |
da048e3d | 1363 | { |
2b328fc9 RR |
1364 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
1365 | ||
1366 | if (window->default_widget) | |
1367 | { | |
1368 | gtk_widget_activate (window->default_widget); | |
1369 | return; | |
1370 | } | |
13111b2a | 1371 | } |
da048e3d RR |
1372 | } |
1373 | ||
2830bf19 | 1374 | key_event.Skip(); |
6de97a3b | 1375 | } |
c801d85f | 1376 | |
03f38c58 | 1377 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 1378 | { |
ae0bdb01 | 1379 | return GTK_WIDGET(m_text); |
6de97a3b | 1380 | } |
e3e65dac | 1381 | |
903f689b RR |
1382 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
1383 | { | |
ae0bdb01 | 1384 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1385 | { |
1386 | #ifdef __WXGTK20__ | |
1387 | return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork | |
1388 | #else | |
ae0bdb01 | 1389 | return (window == GTK_TEXT(m_text)->text_area); |
fab591c5 RR |
1390 | #endif |
1391 | } | |
ae0bdb01 | 1392 | else |
fab591c5 | 1393 | { |
ae0bdb01 | 1394 | return (window == GTK_ENTRY(m_text)->text_area); |
fab591c5 | 1395 | } |
903f689b | 1396 | } |
e3e65dac | 1397 | |
bb69661b VZ |
1398 | // the font will change for subsequent text insertiongs |
1399 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
868a2826 | 1400 | { |
223d09f6 | 1401 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); |
8bbe427f | 1402 | |
a66954a6 | 1403 | if ( !wxTextCtrlBase::SetFont(font) ) |
bb69661b VZ |
1404 | { |
1405 | // font didn't change, nothing to do | |
1406 | return FALSE; | |
1407 | } | |
1408 | ||
1409 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1410 | { | |
c04ec496 | 1411 | SetUpdateFont(TRUE); |
bb69661b | 1412 | |
1ff4714d VZ |
1413 | m_defaultStyle.SetFont(font); |
1414 | ||
01041145 | 1415 | ChangeFontGlobally(); |
bb69661b VZ |
1416 | } |
1417 | ||
1418 | return TRUE; | |
58614078 RR |
1419 | } |
1420 | ||
01041145 VZ |
1421 | void wxTextCtrl::ChangeFontGlobally() |
1422 | { | |
1423 | // this method is very inefficient and hence should be called as rarely as | |
1424 | // possible! | |
c04ec496 VZ |
1425 | // |
1426 | // TODO: it can be implemented much more efficiently for GTK2 | |
1427 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) | |
1428 | #ifndef __WXGTK20__ | |
1429 | && m_updateFont | |
1430 | #endif // GTK+ 1.x | |
1431 | , | |
01041145 VZ |
1432 | _T("shouldn't be called for single line controls") ); |
1433 | ||
1434 | wxString value = GetValue(); | |
1435 | if ( !value.IsEmpty() ) | |
1436 | { | |
c04ec496 | 1437 | SetUpdateFont(FALSE); |
572aeb77 | 1438 | |
01041145 VZ |
1439 | Clear(); |
1440 | AppendText(value); | |
01041145 VZ |
1441 | } |
1442 | } | |
1443 | ||
c04ec496 VZ |
1444 | #ifndef __WXGTK20__ |
1445 | ||
01041145 VZ |
1446 | void wxTextCtrl::UpdateFontIfNeeded() |
1447 | { | |
1448 | if ( m_updateFont ) | |
1449 | ChangeFontGlobally(); | |
1450 | } | |
1451 | ||
c04ec496 VZ |
1452 | #endif // GTK+ 1.x |
1453 | ||
17665a2b VZ |
1454 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) |
1455 | { | |
1456 | if ( !wxControl::SetForegroundColour(colour) ) | |
1457 | return FALSE; | |
1458 | ||
1459 | // update default fg colour too | |
1460 | m_defaultStyle.SetTextColour(colour); | |
1461 | ||
1462 | return TRUE; | |
1463 | } | |
1464 | ||
f03fc89f | 1465 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
68dda785 | 1466 | { |
223d09f6 | 1467 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); |
a81258be | 1468 | |
1f477433 VZ |
1469 | if ( !wxControl::SetBackgroundColour( colour ) ) |
1470 | return FALSE; | |
3358d36e | 1471 | |
f03fc89f VZ |
1472 | if (!m_widget->window) |
1473 | return FALSE; | |
8bbe427f | 1474 | |
f03fc89f VZ |
1475 | if (!m_backgroundColour.Ok()) |
1476 | return FALSE; | |
8bbe427f | 1477 | |
ae0bdb01 RR |
1478 | if (m_windowStyle & wxTE_MULTILINE) |
1479 | { | |
1fc32b12 | 1480 | #ifndef __WXGTK20__ |
ae0bdb01 | 1481 | GdkWindow *window = GTK_TEXT(m_text)->text_area; |
f03fc89f VZ |
1482 | if (!window) |
1483 | return FALSE; | |
ae0bdb01 RR |
1484 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); |
1485 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
1486 | gdk_window_clear( window ); | |
fab591c5 | 1487 | #endif |
ae0bdb01 | 1488 | } |
f03fc89f | 1489 | |
17665a2b VZ |
1490 | // change active background color too |
1491 | m_defaultStyle.SetBackgroundColour( colour ); | |
1492 | ||
f03fc89f | 1493 | return TRUE; |
58614078 RR |
1494 | } |
1495 | ||
eda40bfc | 1496 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) |
17665a2b | 1497 | { |
17665a2b VZ |
1498 | if ( m_windowStyle & wxTE_MULTILINE ) |
1499 | { | |
1500 | if ( style.IsDefault() ) | |
1501 | { | |
1502 | // nothing to do | |
1503 | return TRUE; | |
1504 | } | |
cc3da3f8 RR |
1505 | #ifdef __WXGTK20__ |
1506 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1507 | gint l = gtk_text_buffer_get_char_count( text_buffer ); | |
17665a2b | 1508 | |
cc3da3f8 RR |
1509 | wxCHECK_MSG( start >= 0 && end <= l, FALSE, |
1510 | _T("invalid range in wxTextCtrl::SetStyle") ); | |
1511 | ||
1512 | GtkTextIter starti, endi; | |
1513 | gtk_text_buffer_get_iter_at_offset( text_buffer, &starti, start ); | |
1514 | gtk_text_buffer_get_iter_at_offset( text_buffer, &endi, end ); | |
1515 | ||
1516 | // use the attributes from style which are set in it and fall back | |
1517 | // first to the default style and then to the text control default | |
1518 | // colours for the others | |
1519 | wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this); | |
1520 | ||
1521 | PangoFontDescription *font_description = attr.HasFont() | |
1522 | ? attr.GetFont().GetNativeFontInfo()->description | |
1523 | : NULL; | |
1524 | ||
1525 | GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor() | |
1526 | : NULL; | |
1527 | ||
1528 | GdkColor *colBg = attr.HasBackgroundColour() | |
1529 | ? attr.GetBackgroundColour().GetColor() | |
1530 | : NULL; | |
1531 | ||
1532 | GtkTextTag *tag; | |
1533 | tag = gtk_text_buffer_create_tag( text_buffer, NULL, "font-desc", font_description, | |
1534 | "foreground-gdk", colFg, | |
1535 | "background-gdk", colBg, NULL ); | |
1536 | gtk_text_buffer_apply_tag( text_buffer, tag, &starti, &endi ); | |
1537 | ||
1538 | return TRUE; | |
1539 | #else | |
1540 | // VERY dirty way to do that - removes the required text and re-adds it | |
1541 | // with styling (FIXME) | |
5b87f8bf | 1542 | |
17665a2b VZ |
1543 | gint l = gtk_text_get_length( GTK_TEXT(m_text) ); |
1544 | ||
1545 | wxCHECK_MSG( start >= 0 && end <= l, FALSE, | |
1546 | _T("invalid range in wxTextCtrl::SetStyle") ); | |
1547 | ||
1548 | gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) ); | |
1549 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end ); | |
1550 | wxString tmp(text,*wxConvCurrent); | |
1551 | g_free( text ); | |
1552 | ||
1553 | gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end ); | |
1554 | gtk_editable_set_position( GTK_EDITABLE(m_text), start ); | |
1555 | ||
1556 | #if wxUSE_UNICODE | |
1557 | wxWX2MBbuf buf = tmp.mbc_str(); | |
1558 | const char *txt = buf; | |
1559 | size_t txtlen = strlen(buf); | |
1560 | #else | |
1561 | const char *txt = tmp; | |
1562 | size_t txtlen = tmp.length(); | |
1563 | #endif | |
1564 | ||
eda40bfc VZ |
1565 | // use the attributes from style which are set in it and fall back |
1566 | // first to the default style and then to the text control default | |
1567 | // colours for the others | |
1568 | wxGtkTextInsert(m_text, | |
1569 | wxTextAttr::Combine(style, m_defaultStyle, this), | |
1570 | txt, | |
1571 | txtlen); | |
17665a2b VZ |
1572 | |
1573 | /* does not seem to help under GTK+ 1.2 !!! | |
1574 | gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */ | |
1575 | SetInsertionPoint( old_pos ); | |
fab591c5 | 1576 | #endif |
17665a2b VZ |
1577 | return TRUE; |
1578 | } | |
1579 | else // singe line | |
1580 | { | |
1581 | // cannot do this for GTK+'s Entry widget | |
1582 | return FALSE; | |
1583 | } | |
1584 | } | |
1585 | ||
f40fdaa3 | 1586 | void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) |
58614078 | 1587 | { |
f40fdaa3 | 1588 | gtk_widget_modify_style(m_text, style); |
68dda785 | 1589 | } |
f96aa4d9 | 1590 | |
e702ff0f JS |
1591 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
1592 | { | |
1593 | Cut(); | |
1594 | } | |
1595 | ||
1596 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1597 | { | |
1598 | Copy(); | |
1599 | } | |
1600 | ||
1601 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1602 | { | |
1603 | Paste(); | |
1604 | } | |
1605 | ||
1606 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1607 | { | |
1608 | Undo(); | |
1609 | } | |
1610 | ||
1611 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1612 | { | |
1613 | Redo(); | |
1614 | } | |
1615 | ||
1616 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1617 | { | |
1618 | event.Enable( CanCut() ); | |
1619 | } | |
1620 | ||
1621 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1622 | { | |
1623 | event.Enable( CanCopy() ); | |
1624 | } | |
1625 | ||
1626 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1627 | { | |
1628 | event.Enable( CanPaste() ); | |
1629 | } | |
1630 | ||
1631 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1632 | { | |
1633 | event.Enable( CanUndo() ); | |
1634 | } | |
1635 | ||
1636 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1637 | { | |
1638 | event.Enable( CanRedo() ); | |
1639 | } | |
65045edd RR |
1640 | |
1641 | void wxTextCtrl::OnInternalIdle() | |
1642 | { | |
1643 | wxCursor cursor = m_cursor; | |
1644 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
1645 | ||
307f16e8 | 1646 | if (cursor.Ok()) |
65045edd | 1647 | { |
fab591c5 | 1648 | #ifndef __WXGTK20__ |
65045edd | 1649 | GdkWindow *window = (GdkWindow*) NULL; |
13111b2a | 1650 | if (HasFlag(wxTE_MULTILINE)) |
65045edd RR |
1651 | window = GTK_TEXT(m_text)->text_area; |
1652 | else | |
1653 | window = GTK_ENTRY(m_text)->text_area; | |
13111b2a | 1654 | |
65045edd RR |
1655 | if (window) |
1656 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
1657 | ||
1658 | if (!g_globalCursor.Ok()) | |
1659 | cursor = *wxSTANDARD_CURSOR; | |
1660 | ||
1661 | window = m_widget->window; | |
247e5b16 | 1662 | if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget))) |
65045edd | 1663 | gdk_window_set_cursor( window, cursor.GetCursor() ); |
fab591c5 | 1664 | #endif |
65045edd | 1665 | } |
26bf1ce0 | 1666 | |
d7fa7eaa RR |
1667 | if (g_delayedFocus == this) |
1668 | { | |
1669 | if (GTK_WIDGET_REALIZED(m_widget)) | |
1670 | { | |
1671 | gtk_widget_grab_focus( m_widget ); | |
1672 | g_delayedFocus = NULL; | |
1673 | } | |
1674 | } | |
1675 | ||
e39af974 JS |
1676 | if (wxUpdateUIEvent::CanUpdate(this)) |
1677 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
65045edd | 1678 | } |
f68586e5 VZ |
1679 | |
1680 | wxSize wxTextCtrl::DoGetBestSize() const | |
1681 | { | |
1682 | // FIXME should be different for multi-line controls... | |
0279e844 | 1683 | wxSize ret( wxControl::DoGetBestSize() ); |
9f884528 RD |
1684 | wxSize best(80, ret.y); |
1685 | CacheBestSize(best); | |
1686 | return best; | |
f68586e5 | 1687 | } |
0cc7251e | 1688 | |
9cd6d737 VZ |
1689 | // ---------------------------------------------------------------------------- |
1690 | // freeze/thaw | |
1691 | // ---------------------------------------------------------------------------- | |
1692 | ||
0cc7251e VZ |
1693 | void wxTextCtrl::Freeze() |
1694 | { | |
fab591c5 | 1695 | #ifndef __WXGTK20__ |
0cc7251e VZ |
1696 | if ( HasFlag(wxTE_MULTILINE) ) |
1697 | { | |
1698 | gtk_text_freeze(GTK_TEXT(m_text)); | |
1699 | } | |
fab591c5 | 1700 | #endif |
0cc7251e VZ |
1701 | } |
1702 | ||
1703 | void wxTextCtrl::Thaw() | |
1704 | { | |
fab591c5 | 1705 | #ifndef __WXGTK20__ |
0cc7251e VZ |
1706 | if ( HasFlag(wxTE_MULTILINE) ) |
1707 | { | |
817ec43a VZ |
1708 | GTK_TEXT(m_text)->vadj->value = 0.0; |
1709 | ||
0cc7251e VZ |
1710 | gtk_text_thaw(GTK_TEXT(m_text)); |
1711 | } | |
fab591c5 | 1712 | #endif |
0cc7251e | 1713 | } |
9cd6d737 VZ |
1714 | |
1715 | // ---------------------------------------------------------------------------- | |
1716 | // scrolling | |
1717 | // ---------------------------------------------------------------------------- | |
1718 | ||
1719 | GtkAdjustment *wxTextCtrl::GetVAdj() const | |
1720 | { | |
fab591c5 RR |
1721 | #ifdef __WXGTK20__ |
1722 | return NULL; | |
1723 | #else | |
9cd6d737 | 1724 | return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL; |
fab591c5 | 1725 | #endif |
9cd6d737 VZ |
1726 | } |
1727 | ||
1728 | bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff) | |
1729 | { | |
fab591c5 | 1730 | #ifndef __WXGTK20__ |
9cd6d737 VZ |
1731 | float value = adj->value + diff; |
1732 | ||
1733 | if ( value < 0 ) | |
1734 | value = 0; | |
1735 | ||
1736 | float upper = adj->upper - adj->page_size; | |
1737 | if ( value > upper ) | |
1738 | value = upper; | |
1739 | ||
1740 | // did we noticeably change the scroll position? | |
1741 | if ( fabs(adj->value - value) < 0.2 ) | |
1742 | { | |
1743 | // well, this is what Robert does in wxScrollBar, so it must be good... | |
1744 | return FALSE; | |
1745 | } | |
1746 | ||
1747 | adj->value = value; | |
1748 | ||
1749 | gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed"); | |
1750 | ||
fab591c5 | 1751 | #endif |
9cd6d737 VZ |
1752 | return TRUE; |
1753 | } | |
1754 | ||
1755 | bool wxTextCtrl::ScrollLines(int lines) | |
1756 | { | |
fab591c5 RR |
1757 | #ifdef __WXGTK20__ |
1758 | return FALSE; | |
1759 | #else | |
9cd6d737 VZ |
1760 | GtkAdjustment *adj = GetVAdj(); |
1761 | if ( !adj ) | |
1762 | return FALSE; | |
1763 | ||
1764 | // this is hardcoded to 10 in GTK+ 1.2 (great idea) | |
1765 | static const int KEY_SCROLL_PIXELS = 10; | |
1766 | ||
1767 | return DoScroll(adj, lines*KEY_SCROLL_PIXELS); | |
fab591c5 | 1768 | #endif |
9cd6d737 VZ |
1769 | } |
1770 | ||
1771 | bool wxTextCtrl::ScrollPages(int pages) | |
1772 | { | |
fab591c5 RR |
1773 | #ifdef __WXGTK20__ |
1774 | return FALSE; | |
1775 | #else | |
9cd6d737 VZ |
1776 | GtkAdjustment *adj = GetVAdj(); |
1777 | if ( !adj ) | |
1778 | return FALSE; | |
1779 | ||
817ec43a | 1780 | return DoScroll(adj, (int)ceil(pages*adj->page_increment)); |
fab591c5 | 1781 | #endif |
9cd6d737 VZ |
1782 | } |
1783 | ||
9d522606 RD |
1784 | |
1785 | // static | |
1786 | wxVisualAttributes | |
1787 | wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1788 | { | |
1789 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
1790 | } |