]>
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 | |
39ccbf9a VZ |
521 | if ( buffer ) |
522 | tmp = buffer; | |
a8bf1826 | 523 | |
fab591c5 RR |
524 | g_free( text ); |
525 | #else | |
2830bf19 RR |
526 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
527 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
fab591c5 | 528 | tmp = text; |
2830bf19 | 529 | g_free( text ); |
fab591c5 | 530 | #endif |
2830bf19 RR |
531 | } |
532 | else | |
533 | { | |
fab591c5 | 534 | tmp = wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text) ) ); |
2830bf19 | 535 | } |
a8bf1826 | 536 | |
2830bf19 | 537 | return tmp; |
6de97a3b | 538 | } |
c801d85f KB |
539 | |
540 | void wxTextCtrl::SetValue( const wxString &value ) | |
541 | { | |
223d09f6 | 542 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 543 | |
2830bf19 RR |
544 | if (m_windowStyle & wxTE_MULTILINE) |
545 | { | |
fab591c5 RR |
546 | #ifdef __WXGTK20__ |
547 | ||
548 | #if wxUSE_UNICODE | |
549 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( value) ); | |
550 | #else | |
551 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( value ) ) ); | |
a8bf1826 | 552 | #endif |
fab591c5 RR |
553 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
554 | gtk_text_buffer_set_text( text_buffer, buffer, strlen(buffer) ); | |
a8bf1826 | 555 | |
fab591c5 | 556 | #else |
2830bf19 RR |
557 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
558 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
559 | len = 0; | |
01041145 | 560 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value.mbc_str(), value.Length(), &len ); |
05939a81 | 561 | #endif |
2830bf19 RR |
562 | } |
563 | else | |
564 | { | |
fab591c5 | 565 | gtk_entry_set_text( GTK_ENTRY(m_text), wxGTK_CONV( value ) ); |
2830bf19 | 566 | } |
f6bcfd97 BP |
567 | |
568 | // GRG, Jun/2000: Changed this after a lot of discussion in | |
77ffb593 | 569 | // the lists. wxWidgets 2.2 will have a set of flags to |
f6bcfd97 BP |
570 | // customize this behaviour. |
571 | SetInsertionPoint(0); | |
a8bf1826 | 572 | |
f6bcfd97 | 573 | m_modified = FALSE; |
6de97a3b | 574 | } |
c801d85f KB |
575 | |
576 | void wxTextCtrl::WriteText( const wxString &text ) | |
577 | { | |
223d09f6 | 578 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 579 | |
17665a2b VZ |
580 | if ( text.empty() ) |
581 | return; | |
484e45bf | 582 | |
c04ec496 VZ |
583 | // gtk_text_changed_callback() will set m_modified to true but m_modified |
584 | // shouldn't be changed by the program writing to the text control itself, | |
585 | // so save the old value and restore when we're done | |
586 | bool oldModified = m_modified; | |
587 | ||
17665a2b | 588 | if ( m_windowStyle & wxTE_MULTILINE ) |
2830bf19 | 589 | { |
fab591c5 RR |
590 | #ifdef __WXGTK20__ |
591 | ||
592 | #if wxUSE_UNICODE | |
593 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) ); | |
594 | #else | |
595 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) ); | |
a8bf1826 | 596 | #endif |
8e033d81 VZ |
597 | if ( !buffer ) |
598 | { | |
599 | // what else can we do? at least don't crash... | |
600 | return; | |
601 | } | |
602 | ||
fab591c5 | 603 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
5b87f8bf | 604 | |
e136b747 | 605 | // TODO: Call whatever is needed to delete the selection. |
cc3da3f8 | 606 | wxGtkTextInsert( m_text, text_buffer, m_defaultStyle, buffer ); |
a8bf1826 | 607 | |
71aba833 VZ |
608 | GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(m_widget) ); |
609 | // Scroll to cursor, but only if scrollbar thumb is at the very bottom | |
610 | if ( adj->value == adj->upper - adj->page_size ) | |
611 | { | |
612 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), | |
613 | gtk_text_buffer_get_insert( text_buffer ), 0.0, FALSE, 0.0, 1.0 ); | |
614 | } | |
a8bf1826 | 615 | #else // GTK 1.x |
ba3f6b44 | 616 | // After cursor movements, gtk_text_get_point() is wrong by one. |
9e691f46 | 617 | gtk_text_set_point( GTK_TEXT(m_text), GET_EDITABLE_POS(m_text) ); |
eda40bfc | 618 | |
41b2e0e6 VZ |
619 | // always use m_defaultStyle, even if it is empty as otherwise |
620 | // resetting the style and appending some more text wouldn't work: if | |
621 | // we don't specify the style explicitly, the old style would be used | |
2b5f62a0 | 622 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); |
fab591c5 | 623 | wxGtkTextInsert(m_text, m_defaultStyle, text.c_str(), text.Len()); |
eda40bfc | 624 | |
07e9d4f0 VZ |
625 | // we called wxGtkTextInsert with correct font, no need to do anything |
626 | // in UpdateFontIfNeeded() any longer | |
627 | if ( !text.empty() ) | |
628 | { | |
c04ec496 | 629 | SetUpdateFont(FALSE); |
07e9d4f0 VZ |
630 | } |
631 | ||
ba3f6b44 | 632 | // Bring editable's cursor back uptodate. |
9e691f46 | 633 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
a8bf1826 | 634 | #endif // GTK 1.x/2.0 |
2830bf19 | 635 | } |
17665a2b | 636 | else // single line |
2830bf19 | 637 | { |
2b5f62a0 VZ |
638 | // First remove the selection if there is one |
639 | gtk_editable_delete_selection( GTK_EDITABLE(m_text) ); | |
640 | ||
ba3f6b44 | 641 | // This moves the cursor pos to behind the inserted text. |
9e691f46 | 642 | gint len = GET_EDITABLE_POS(m_text); |
a8bf1826 | 643 | |
fab591c5 RR |
644 | #ifdef __WXGTK20__ |
645 | ||
646 | #if wxUSE_UNICODE | |
647 | wxCharBuffer buffer( wxConvUTF8.cWX2MB( text ) ); | |
648 | #else | |
649 | wxCharBuffer buffer( wxConvUTF8.cWC2MB( wxConvLocal.cWX2WC( text ) ) ); | |
a8bf1826 | 650 | #endif |
fab591c5 | 651 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buffer, strlen(buffer), &len ); |
a8bf1826 | 652 | |
fab591c5 RR |
653 | #else |
654 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text.c_str(), text.Len(), &len ); | |
655 | #endif | |
3358d36e | 656 | |
ba3f6b44 | 657 | // Bring entry's cursor uptodate. |
9e691f46 | 658 | gtk_entry_set_position( GTK_ENTRY(m_text), len ); |
2830bf19 | 659 | } |
eda40bfc | 660 | |
c04ec496 | 661 | m_modified = oldModified; |
6de97a3b | 662 | } |
c801d85f | 663 | |
a6e21573 HH |
664 | void wxTextCtrl::AppendText( const wxString &text ) |
665 | { | |
ba3f6b44 RR |
666 | SetInsertionPointEnd(); |
667 | WriteText( text ); | |
668 | } | |
2df7be7f | 669 | |
ba3f6b44 RR |
670 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
671 | { | |
a6e21573 HH |
672 | if (m_windowStyle & wxTE_MULTILINE) |
673 | { | |
fab591c5 | 674 | #ifndef __WXGTK20__ |
ba3f6b44 RR |
675 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
676 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
bb69661b | 677 | |
ba3f6b44 RR |
678 | if (text) |
679 | { | |
680 | wxString buf(wxT("")); | |
681 | long i; | |
682 | int currentLine = 0; | |
683 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
684 | if (text[i] == '\n') | |
685 | currentLine++; | |
686 | // Now get the text | |
687 | int j; | |
688 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
689 | buf += text[i]; | |
17665a2b | 690 | |
ba3f6b44 RR |
691 | g_free( text ); |
692 | return buf; | |
bb69661b | 693 | } |
ba3f6b44 | 694 | else |
bb69661b | 695 | { |
ba3f6b44 | 696 | return wxEmptyString; |
bb69661b | 697 | } |
905f2110 JS |
698 | #else |
699 | GtkTextBuffer *text_buffer; | |
700 | text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(m_text)); | |
701 | GtkTextIter line; | |
702 | gtk_text_buffer_get_iter_at_line(text_buffer,&line,lineNo); | |
703 | GtkTextIter end; | |
704 | gtk_text_buffer_get_end_iter(text_buffer,&end ); | |
705 | gchar *text = gtk_text_buffer_get_text(text_buffer,&line,&end,TRUE); | |
58192970 | 706 | wxString result(wxGTK_CONV_BACK(text)); |
905f2110 JS |
707 | g_free(text); |
708 | return result.BeforeFirst(wxT('\n')); | |
709 | #endif | |
a6e21573 | 710 | } |
ba3f6b44 | 711 | else |
a81258be | 712 | { |
ba3f6b44 RR |
713 | if (lineNo == 0) return GetValue(); |
714 | return wxEmptyString; | |
a81258be | 715 | } |
6de97a3b | 716 | } |
c801d85f | 717 | |
a81258be RR |
718 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
719 | { | |
ac0d36b5 HH |
720 | /* If you implement this, don't forget to update the documentation! |
721 | * (file docs/latex/wx/text.tex) */ | |
223d09f6 | 722 | wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") ); |
a81258be | 723 | } |
112892b9 | 724 | |
0efe5ba7 | 725 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 726 | { |
96385642 | 727 | if ( m_windowStyle & wxTE_MULTILINE ) |
805dd538 | 728 | { |
96385642 VZ |
729 | wxString text = GetValue(); |
730 | ||
6085b116 | 731 | // cast to prevent warning. But pos really should've been unsigned. |
2829d9e3 | 732 | if( (unsigned long)pos > text.Len() ) |
96385642 VZ |
733 | return FALSE; |
734 | ||
ac0d36b5 HH |
735 | *x=0; // First Col |
736 | *y=0; // First Line | |
2829d9e3 | 737 | |
05939a81 OK |
738 | const wxChar* stop = text.c_str() + pos; |
739 | for ( const wxChar *p = text.c_str(); p < stop; p++ ) | |
96385642 | 740 | { |
223d09f6 | 741 | if (*p == wxT('\n')) |
96385642 VZ |
742 | { |
743 | (*y)++; | |
ac0d36b5 | 744 | *x=0; |
96385642 VZ |
745 | } |
746 | else | |
747 | (*x)++; | |
748 | } | |
805dd538 | 749 | } |
96385642 VZ |
750 | else // single line control |
751 | { | |
2829d9e3 | 752 | if ( pos <= GTK_ENTRY(m_text)->text_length ) |
96385642 | 753 | { |
ac0d36b5 | 754 | *y = 0; |
96385642 VZ |
755 | *x = pos; |
756 | } | |
757 | else | |
758 | { | |
759 | // index out of bounds | |
760 | return FALSE; | |
761 | } | |
8bbe427f | 762 | } |
96385642 VZ |
763 | |
764 | return TRUE; | |
6de97a3b | 765 | } |
c801d85f | 766 | |
e3ca08dd | 767 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 768 | { |
2830bf19 | 769 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; |
805dd538 | 770 | |
2830bf19 | 771 | long pos=0; |
ac0d36b5 | 772 | for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n' |
8bbe427f | 773 | |
3358d36e | 774 | pos += x; |
2830bf19 | 775 | return pos; |
6de97a3b | 776 | } |
c801d85f | 777 | |
a81258be | 778 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 779 | { |
a81258be RR |
780 | wxString str = GetLineText (lineNo); |
781 | return (int) str.Length(); | |
6de97a3b | 782 | } |
c801d85f | 783 | |
a81258be | 784 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 785 | { |
2830bf19 | 786 | if (m_windowStyle & wxTE_MULTILINE) |
a81258be | 787 | { |
fab591c5 RR |
788 | #ifdef __WXGTK20__ |
789 | GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
a8bf1826 | 790 | |
fab591c5 RR |
791 | return gtk_text_buffer_get_line_count( buffer ); |
792 | #else | |
2830bf19 RR |
793 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
794 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
795 | ||
796 | if (text) | |
797 | { | |
798 | int currentLine = 0; | |
799 | for (int i = 0; i < len; i++ ) | |
96385642 | 800 | { |
2830bf19 | 801 | if (text[i] == '\n') |
96385642 VZ |
802 | currentLine++; |
803 | } | |
2830bf19 | 804 | g_free( text ); |
2829d9e3 VZ |
805 | |
806 | // currentLine is 0 based, add 1 to get number of lines | |
807 | return currentLine + 1; | |
2830bf19 RR |
808 | } |
809 | else | |
96385642 | 810 | { |
2830bf19 | 811 | return 0; |
96385642 | 812 | } |
fab591c5 | 813 | #endif |
a81258be RR |
814 | } |
815 | else | |
2830bf19 | 816 | { |
96385642 | 817 | return 1; |
2830bf19 | 818 | } |
6de97a3b | 819 | } |
c801d85f | 820 | |
debe6624 | 821 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 822 | { |
223d09f6 | 823 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3358d36e | 824 | |
74c5a810 | 825 | if ( IsMultiLine() ) |
291a8f20 | 826 | { |
fab591c5 RR |
827 | #ifdef __WXGTK20__ |
828 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
829 | GtkTextIter iter; | |
830 | gtk_text_buffer_get_iter_at_offset( text_buffer, &iter, pos ); | |
831 | gtk_text_buffer_place_cursor( text_buffer, &iter ); | |
74c5a810 VZ |
832 | gtk_text_view_scroll_mark_onscreen |
833 | ( | |
834 | GTK_TEXT_VIEW(m_text), | |
835 | gtk_text_buffer_get_insert( text_buffer ) | |
836 | ); | |
837 | #else // GTK+ 1.x | |
291a8f20 RR |
838 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_text), |
839 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e | 840 | |
291a8f20 | 841 | /* we fake a set_point by inserting and deleting. as the user |
fab591c5 | 842 | isn't supposed to get to know about this non-sense, we |
3358d36e VZ |
843 | disconnect so that no events are sent to the user program. */ |
844 | ||
291a8f20 RR |
845 | gint tmp = (gint)pos; |
846 | gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp ); | |
3358d36e VZ |
847 | gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp ); |
848 | ||
291a8f20 RR |
849 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
850 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e | 851 | |
fab591c5 | 852 | // bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 853 | SET_EDITABLE_POS(m_text, gtk_text_get_point( GTK_TEXT(m_text) )); |
74c5a810 | 854 | #endif // GTK+ 2/1 |
ac0d36b5 | 855 | } |
2830bf19 | 856 | else |
291a8f20 | 857 | { |
d59051dd | 858 | gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos ); |
3358d36e | 859 | |
fab591c5 | 860 | // Bring editable's cursor uptodate. Bug in GTK. |
9e691f46 | 861 | SET_EDITABLE_POS(m_text, (guint32)pos); |
291a8f20 | 862 | } |
6de97a3b | 863 | } |
c801d85f | 864 | |
03f38c58 | 865 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 866 | { |
223d09f6 | 867 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 868 | |
d59051dd | 869 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
870 | { |
871 | #ifdef __WXGTK20__ | |
872 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
873 | GtkTextIter end; | |
874 | gtk_text_buffer_get_end_iter( text_buffer, &end ); | |
875 | gtk_text_buffer_place_cursor( text_buffer, &end ); | |
876 | #else | |
d59051dd | 877 | SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text))); |
fab591c5 RR |
878 | #endif |
879 | } | |
d59051dd | 880 | else |
fab591c5 | 881 | { |
d59051dd | 882 | gtk_entry_set_position( GTK_ENTRY(m_text), -1 ); |
fab591c5 | 883 | } |
6de97a3b | 884 | } |
c801d85f | 885 | |
debe6624 | 886 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 887 | { |
223d09f6 | 888 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 889 | |
2830bf19 | 890 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
891 | { |
892 | #ifdef __WXGTK20__ | |
893 | gtk_text_view_set_editable( GTK_TEXT_VIEW(m_text), editable ); | |
894 | #else | |
2830bf19 | 895 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); |
fab591c5 RR |
896 | #endif |
897 | } | |
2830bf19 | 898 | else |
fab591c5 | 899 | { |
2830bf19 | 900 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); |
fab591c5 | 901 | } |
6de97a3b | 902 | } |
c801d85f | 903 | |
68df5777 RR |
904 | bool wxTextCtrl::Enable( bool enable ) |
905 | { | |
906 | if (!wxWindowBase::Enable(enable)) | |
907 | { | |
908 | // nothing to do | |
909 | return FALSE; | |
910 | } | |
f6bcfd97 | 911 | |
68df5777 RR |
912 | if (m_windowStyle & wxTE_MULTILINE) |
913 | { | |
fab591c5 RR |
914 | #ifdef __WXGTK20__ |
915 | SetEditable( enable ); | |
916 | #else | |
68df5777 | 917 | gtk_text_set_editable( GTK_TEXT(m_text), enable ); |
5abf8c9d | 918 | OnParentEnable(enable); |
fab591c5 | 919 | #endif |
68df5777 RR |
920 | } |
921 | else | |
922 | { | |
923 | gtk_widget_set_sensitive( m_text, enable ); | |
924 | } | |
925 | ||
926 | return TRUE; | |
927 | } | |
928 | ||
fdca68a6 JS |
929 | // wxGTK-specific: called recursively by Enable, |
930 | // to give widgets an oppprtunity to correct their colours after they | |
931 | // have been changed by Enable | |
932 | void wxTextCtrl::OnParentEnable( bool enable ) | |
933 | { | |
934 | // If we have a custom background colour, we use this colour in both | |
935 | // disabled and enabled mode, or we end up with a different colour under the | |
936 | // text. | |
937 | wxColour oldColour = GetBackgroundColour(); | |
938 | if (oldColour.Ok()) | |
939 | { | |
940 | // Need to set twice or it'll optimize the useful stuff out | |
941 | if (oldColour == * wxWHITE) | |
942 | SetBackgroundColour(*wxBLACK); | |
943 | else | |
944 | SetBackgroundColour(*wxWHITE); | |
945 | SetBackgroundColour(oldColour); | |
946 | } | |
947 | } | |
948 | ||
3a9fa0d6 VZ |
949 | void wxTextCtrl::MarkDirty() |
950 | { | |
951 | m_modified = TRUE; | |
952 | } | |
953 | ||
0efe5ba7 VZ |
954 | void wxTextCtrl::DiscardEdits() |
955 | { | |
956 | m_modified = FALSE; | |
957 | } | |
958 | ||
ce2f50e3 VZ |
959 | // ---------------------------------------------------------------------------- |
960 | // max text length support | |
961 | // ---------------------------------------------------------------------------- | |
962 | ||
963 | void wxTextCtrl::IgnoreNextTextUpdate() | |
964 | { | |
965 | m_ignoreNextUpdate = TRUE; | |
966 | } | |
967 | ||
968 | bool wxTextCtrl::IgnoreTextUpdate() | |
969 | { | |
970 | if ( m_ignoreNextUpdate ) | |
971 | { | |
972 | m_ignoreNextUpdate = FALSE; | |
973 | ||
974 | return TRUE; | |
975 | } | |
976 | ||
977 | return FALSE; | |
978 | } | |
979 | ||
d7eee191 VZ |
980 | void wxTextCtrl::SetMaxLength(unsigned long len) |
981 | { | |
982 | if ( !HasFlag(wxTE_MULTILINE) ) | |
983 | { | |
984 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
ce2f50e3 VZ |
985 | |
986 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
987 | // we had tried to enter more text than allowed by max text length and | |
988 | // the text wasn't really changed | |
989 | // | |
990 | // to detect this and generate TEXT_MAXLEN event instead of | |
991 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
992 | // | |
993 | // when max len is set to 0 we disconnect our handler as it means that | |
994 | // we shouldn't check anything any more | |
995 | if ( len ) | |
996 | { | |
997 | gtk_signal_connect( GTK_OBJECT(m_text), | |
998 | "insert_text", | |
999 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
1000 | (gpointer)this); | |
1001 | } | |
1002 | else // no checking | |
1003 | { | |
1004 | gtk_signal_disconnect_by_func | |
1005 | ( | |
1006 | GTK_OBJECT(m_text), | |
1007 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
1008 | (gpointer)this | |
1009 | ); | |
1010 | } | |
d7eee191 VZ |
1011 | } |
1012 | } | |
1013 | ||
debe6624 | 1014 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 1015 | { |
223d09f6 | 1016 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1017 | |
2b5f62a0 VZ |
1018 | if (from == -1 && to == -1) |
1019 | { | |
1020 | from = 0; | |
1021 | to = GetValue().Length(); | |
1022 | } | |
1023 | ||
fab591c5 | 1024 | #ifndef __WXGTK20__ |
a1f79c1e VZ |
1025 | if ( (m_windowStyle & wxTE_MULTILINE) && |
1026 | !GTK_TEXT(m_text)->line_start_cache ) | |
1027 | { | |
1028 | // tell the programmer that it didn't work | |
1029 | wxLogDebug(_T("Can't call SetSelection() before realizing the control")); | |
1030 | return; | |
1031 | } | |
fab591c5 | 1032 | #endif |
a1f79c1e | 1033 | |
fab591c5 RR |
1034 | if (m_windowStyle & wxTE_MULTILINE) |
1035 | { | |
1036 | #ifdef __WXGTK20__ | |
739e366a JS |
1037 | GtkTextBuffer *buf = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); |
1038 | ||
1039 | GtkTextIter fromi, toi; | |
1040 | gtk_text_buffer_get_iter_at_offset( buf, &fromi, from ); | |
1041 | gtk_text_buffer_get_iter_at_offset( buf, &toi, to ); | |
1042 | ||
1043 | gtk_text_buffer_place_cursor( buf, &toi ); | |
1044 | gtk_text_buffer_move_mark_by_name( buf, "selection_bound", &fromi ); | |
fab591c5 RR |
1045 | #else |
1046 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1047 | #endif | |
1048 | } | |
1049 | else | |
1050 | { | |
1051 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
1052 | } | |
6de97a3b | 1053 | } |
c801d85f | 1054 | |
afbe906a | 1055 | void wxTextCtrl::ShowPosition( long pos ) |
c801d85f | 1056 | { |
afbe906a RR |
1057 | if (m_windowStyle & wxTE_MULTILINE) |
1058 | { | |
71aba833 VZ |
1059 | #ifdef __WXGTK20__ |
1060 | GtkTextBuffer *buf = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1061 | GtkTextIter iter; | |
1062 | gtk_text_buffer_get_start_iter( buf, &iter ); | |
1063 | gtk_text_iter_set_offset( &iter, pos ); | |
1064 | GtkTextMark *mark = gtk_text_buffer_create_mark( buf, NULL, &iter, TRUE ); | |
1065 | gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW(m_text), mark, 0.0, FALSE, 0.0, 0.0 ); | |
1066 | #else // GTK 1.x | |
afbe906a RR |
1067 | GtkAdjustment *vp = GTK_TEXT(m_text)->vadj; |
1068 | float totalLines = (float) GetNumberOfLines(); | |
1069 | long posX; | |
1070 | long posY; | |
1071 | PositionToXY(pos, &posX, &posY); | |
1072 | float posLine = (float) posY; | |
1073 | float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower; | |
1074 | gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p); | |
71aba833 | 1075 | #endif // GTK 1.x/2.x |
afbe906a | 1076 | } |
6de97a3b | 1077 | } |
c801d85f | 1078 | |
692c9b86 VZ |
1079 | #ifdef __WXGTK20__ |
1080 | ||
1081 | wxTextCtrlHitTestResult | |
1082 | wxTextCtrl::HitTest(const wxPoint& pt, long *pos) const | |
1083 | { | |
1084 | if ( !IsMultiLine() ) | |
1085 | { | |
1086 | // not supported | |
1087 | return wxTE_HT_UNKNOWN; | |
1088 | } | |
1089 | ||
1090 | int x, y; | |
1091 | gtk_text_view_window_to_buffer_coords | |
1092 | ( | |
1093 | GTK_TEXT_VIEW(m_text), | |
1094 | GTK_TEXT_WINDOW_TEXT, | |
1095 | pt.x, pt.y, | |
1096 | &x, &y | |
1097 | ); | |
1098 | ||
1099 | GtkTextIter iter; | |
1100 | gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(m_text), &iter, x, y); | |
1101 | if ( pos ) | |
1102 | *pos = gtk_text_iter_get_offset(&iter); | |
1103 | ||
1104 | return wxTE_HT_ON_TEXT; | |
1105 | } | |
1106 | ||
1107 | #endif // __WXGTK20__ | |
1108 | ||
03f38c58 | 1109 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 1110 | { |
223d09f6 | 1111 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1112 | |
fab591c5 RR |
1113 | #ifdef __WXGTK20__ |
1114 | if (m_windowStyle & wxTE_MULTILINE) | |
1115 | { | |
1116 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
a8bf1826 | 1117 | |
fab591c5 RR |
1118 | // There is no direct accessor for the cursor, but |
1119 | // internally, the cursor is the "mark" called | |
1120 | // "insert" in the text view's btree structure. | |
a8bf1826 | 1121 | |
fab591c5 RR |
1122 | GtkTextMark *mark = gtk_text_buffer_get_insert( text_buffer ); |
1123 | GtkTextIter cursor; | |
1124 | gtk_text_buffer_get_iter_at_mark( text_buffer, &cursor, mark ); | |
a8bf1826 | 1125 | |
fab591c5 RR |
1126 | return gtk_text_iter_get_offset( &cursor ); |
1127 | } | |
1128 | else | |
1129 | #endif | |
1130 | { | |
9e691f46 | 1131 | return (long) GET_EDITABLE_POS(m_text); |
fab591c5 | 1132 | } |
6de97a3b | 1133 | } |
c801d85f | 1134 | |
03f38c58 | 1135 | long wxTextCtrl::GetLastPosition() const |
c801d85f | 1136 | { |
223d09f6 | 1137 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 1138 | |
2830bf19 | 1139 | int pos = 0; |
a8bf1826 | 1140 | |
2830bf19 | 1141 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1142 | { |
1143 | #ifdef __WXGTK20__ | |
1144 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1145 | GtkTextIter end; | |
1146 | gtk_text_buffer_get_end_iter( text_buffer, &end ); | |
a8bf1826 | 1147 | |
fab591c5 RR |
1148 | pos = gtk_text_iter_get_offset( &end ); |
1149 | #else | |
2830bf19 | 1150 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
fab591c5 RR |
1151 | #endif |
1152 | } | |
2830bf19 | 1153 | else |
fab591c5 | 1154 | { |
2830bf19 | 1155 | pos = GTK_ENTRY(m_text)->text_length; |
fab591c5 | 1156 | } |
805dd538 | 1157 | |
ac0d36b5 | 1158 | return (long)pos; |
6de97a3b | 1159 | } |
c801d85f | 1160 | |
debe6624 | 1161 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 1162 | { |
223d09f6 | 1163 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1164 | |
581ee8a9 | 1165 | #ifdef __WXGTK20__ |
fdd55287 | 1166 | if (m_windowStyle & wxTE_MULTILINE) |
581ee8a9 VZ |
1167 | { |
1168 | GtkTextBuffer * | |
1169 | text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1170 | ||
1171 | GtkTextIter fromi, toi; | |
1172 | gtk_text_buffer_get_iter_at_offset( text_buffer, &fromi, from ); | |
1173 | gtk_text_buffer_get_iter_at_offset( text_buffer, &toi, to ); | |
1174 | ||
1175 | gtk_text_buffer_delete( text_buffer, &fromi, &toi ); | |
1176 | } | |
1177 | else // single line | |
fab591c5 | 1178 | #endif |
581ee8a9 | 1179 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 1180 | } |
c801d85f | 1181 | |
debe6624 | 1182 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 1183 | { |
223d09f6 | 1184 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1185 | |
581ee8a9 | 1186 | Remove( from, to ); |
bb69661b | 1187 | |
2df7be7f RR |
1188 | if (!value.IsEmpty()) |
1189 | { | |
581ee8a9 VZ |
1190 | #ifdef __WXGTK20__ |
1191 | SetInsertionPoint( from ); | |
1192 | WriteText( value ); | |
1193 | #else // GTK 1.x | |
2df7be7f | 1194 | gint pos = (gint)from; |
05939a81 | 1195 | #if wxUSE_UNICODE |
2df7be7f RR |
1196 | wxWX2MBbuf buf = value.mbc_str(); |
1197 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos ); | |
05939a81 | 1198 | #else |
2df7be7f | 1199 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
581ee8a9 VZ |
1200 | #endif // wxUSE_UNICODE |
1201 | #endif // GTK 1.x/2.x | |
2df7be7f | 1202 | } |
6de97a3b | 1203 | } |
c801d85f | 1204 | |
03f38c58 | 1205 | void wxTextCtrl::Cut() |
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, "cut-clipboard"); | |
1212 | else | |
fab591c5 | 1213 | #endif |
bbde2e29 | 1214 | gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1215 | } |
c801d85f | 1216 | |
03f38c58 | 1217 | void wxTextCtrl::Copy() |
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, "copy-clipboard"); | |
1224 | else | |
fab591c5 | 1225 | #endif |
bbde2e29 | 1226 | gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1227 | } |
c801d85f | 1228 | |
03f38c58 | 1229 | void wxTextCtrl::Paste() |
c801d85f | 1230 | { |
223d09f6 | 1231 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 1232 | |
bbde2e29 VS |
1233 | #ifdef __WXGTK20__ |
1234 | if (m_windowStyle & wxTE_MULTILINE) | |
1235 | g_signal_emit_by_name(m_text, "paste-clipboard"); | |
1236 | else | |
fab591c5 | 1237 | #endif |
bbde2e29 | 1238 | gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 1239 | } |
c801d85f | 1240 | |
ca8b28f2 JS |
1241 | // Undo/redo |
1242 | void wxTextCtrl::Undo() | |
1243 | { | |
1244 | // TODO | |
223d09f6 | 1245 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); |
ca8b28f2 JS |
1246 | } |
1247 | ||
1248 | void wxTextCtrl::Redo() | |
1249 | { | |
1250 | // TODO | |
223d09f6 | 1251 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); |
ca8b28f2 JS |
1252 | } |
1253 | ||
1254 | bool wxTextCtrl::CanUndo() const | |
1255 | { | |
1256 | // TODO | |
4855a477 | 1257 | //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); |
ca8b28f2 JS |
1258 | return FALSE; |
1259 | } | |
1260 | ||
1261 | bool wxTextCtrl::CanRedo() const | |
1262 | { | |
1263 | // TODO | |
4855a477 | 1264 | //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); |
ca8b28f2 JS |
1265 | return FALSE; |
1266 | } | |
1267 | ||
1268 | // If the return values from and to are the same, there is no | |
1269 | // selection. | |
2d4cc5b6 | 1270 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const |
ca8b28f2 | 1271 | { |
223d09f6 | 1272 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
bb69661b | 1273 | |
5b87f8bf RD |
1274 | gint from = -1; |
1275 | gint to = -1; | |
1276 | bool haveSelection = FALSE; | |
1277 | ||
fb47b99f | 1278 | #ifdef __WXGTK20__ |
5b87f8bf RD |
1279 | if (m_windowStyle & wxTE_MULTILINE) |
1280 | { | |
1281 | GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (m_text)); | |
1282 | GtkTextIter ifrom, ito; | |
1283 | if ( gtk_text_buffer_get_selection_bounds(buffer, &ifrom, &ito) ) | |
1284 | { | |
1285 | haveSelection = TRUE; | |
1286 | from = gtk_text_iter_get_offset(&ifrom); | |
1287 | to = gtk_text_iter_get_offset(&ito); | |
1288 | } | |
1289 | } | |
1290 | else // not multi-line | |
1291 | { | |
1292 | if ( gtk_editable_get_selection_bounds( GTK_EDITABLE(m_text), | |
1293 | &from, &to) ) | |
1294 | { | |
1295 | haveSelection = TRUE; | |
1296 | } | |
1297 | } | |
1298 | #else // not GTK2 | |
1299 | if ( (GTK_EDITABLE(m_text)->has_selection) ) | |
1300 | { | |
1301 | haveSelection = TRUE; | |
1302 | from = (long) GTK_EDITABLE(m_text)->selection_start_pos; | |
1303 | to = (long) GTK_EDITABLE(m_text)->selection_end_pos; | |
1304 | } | |
9e691f46 | 1305 | #endif |
2d4cc5b6 | 1306 | |
5b87f8bf RD |
1307 | if (! haveSelection ) |
1308 | from = to = GetInsertionPoint(); | |
1309 | ||
1310 | if ( from > to ) | |
1311 | { | |
1312 | // exchange them to be compatible with wxMSW | |
1313 | gint tmp = from; | |
1314 | from = to; | |
1315 | to = tmp; | |
1316 | } | |
bb69661b | 1317 | |
2d4cc5b6 VZ |
1318 | if ( fromOut ) |
1319 | *fromOut = from; | |
1320 | if ( toOut ) | |
1321 | *toOut = to; | |
ca8b28f2 JS |
1322 | } |
1323 | ||
5b87f8bf | 1324 | |
ca8b28f2 JS |
1325 | bool wxTextCtrl::IsEditable() const |
1326 | { | |
223d09f6 | 1327 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); |
05060eeb | 1328 | |
9e691f46 | 1329 | #ifdef __WXGTK20__ |
fdd55287 VZ |
1330 | if (m_windowStyle & wxTE_MULTILINE) |
1331 | { | |
1332 | return gtk_text_view_get_editable(GTK_TEXT_VIEW(m_text)); | |
1333 | } | |
1334 | else | |
1335 | { | |
1336 | return gtk_editable_get_editable(GTK_EDITABLE(m_text)); | |
1337 | } | |
9e691f46 | 1338 | #else |
05060eeb | 1339 | return GTK_EDITABLE(m_text)->editable; |
9e691f46 | 1340 | #endif |
ca8b28f2 JS |
1341 | } |
1342 | ||
0efe5ba7 VZ |
1343 | bool wxTextCtrl::IsModified() const |
1344 | { | |
1345 | return m_modified; | |
1346 | } | |
1347 | ||
03f38c58 | 1348 | void wxTextCtrl::Clear() |
c801d85f | 1349 | { |
223d09f6 | 1350 | SetValue( wxT("") ); |
6de97a3b | 1351 | } |
c801d85f | 1352 | |
903f689b | 1353 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 1354 | { |
223d09f6 | 1355 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
805dd538 | 1356 | |
12a3f227 | 1357 | if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
2830bf19 RR |
1358 | { |
1359 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1360 | event.SetEventObject(this); | |
f6bcfd97 | 1361 | event.SetString(GetValue()); |
2830bf19 RR |
1362 | if (GetEventHandler()->ProcessEvent(event)) return; |
1363 | } | |
903f689b | 1364 | |
12a3f227 | 1365 | if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE)) |
da048e3d | 1366 | { |
2b328fc9 RR |
1367 | // This will invoke the dialog default action, such |
1368 | // as the clicking the default button. | |
a8bf1826 | 1369 | |
da048e3d | 1370 | wxWindow *top_frame = m_parent; |
8487f887 | 1371 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
da048e3d | 1372 | top_frame = top_frame->GetParent(); |
a8bf1826 | 1373 | |
2b328fc9 | 1374 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) |
da048e3d | 1375 | { |
2b328fc9 RR |
1376 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
1377 | ||
1378 | if (window->default_widget) | |
1379 | { | |
1380 | gtk_widget_activate (window->default_widget); | |
1381 | return; | |
1382 | } | |
13111b2a | 1383 | } |
da048e3d RR |
1384 | } |
1385 | ||
2830bf19 | 1386 | key_event.Skip(); |
6de97a3b | 1387 | } |
c801d85f | 1388 | |
03f38c58 | 1389 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 1390 | { |
ae0bdb01 | 1391 | return GTK_WIDGET(m_text); |
6de97a3b | 1392 | } |
e3e65dac | 1393 | |
903f689b RR |
1394 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
1395 | { | |
ae0bdb01 | 1396 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 RR |
1397 | { |
1398 | #ifdef __WXGTK20__ | |
1399 | return window == gtk_text_view_get_window( GTK_TEXT_VIEW( m_text ), GTK_TEXT_WINDOW_TEXT ); // pure guesswork | |
1400 | #else | |
ae0bdb01 | 1401 | return (window == GTK_TEXT(m_text)->text_area); |
fab591c5 RR |
1402 | #endif |
1403 | } | |
ae0bdb01 | 1404 | else |
fab591c5 | 1405 | { |
ae0bdb01 | 1406 | return (window == GTK_ENTRY(m_text)->text_area); |
fab591c5 | 1407 | } |
903f689b | 1408 | } |
e3e65dac | 1409 | |
bb69661b VZ |
1410 | // the font will change for subsequent text insertiongs |
1411 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
868a2826 | 1412 | { |
223d09f6 | 1413 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); |
8bbe427f | 1414 | |
a66954a6 | 1415 | if ( !wxTextCtrlBase::SetFont(font) ) |
bb69661b VZ |
1416 | { |
1417 | // font didn't change, nothing to do | |
1418 | return FALSE; | |
1419 | } | |
1420 | ||
1421 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1422 | { | |
c04ec496 | 1423 | SetUpdateFont(TRUE); |
bb69661b | 1424 | |
1ff4714d VZ |
1425 | m_defaultStyle.SetFont(font); |
1426 | ||
01041145 | 1427 | ChangeFontGlobally(); |
bb69661b VZ |
1428 | } |
1429 | ||
1430 | return TRUE; | |
58614078 RR |
1431 | } |
1432 | ||
01041145 VZ |
1433 | void wxTextCtrl::ChangeFontGlobally() |
1434 | { | |
1435 | // this method is very inefficient and hence should be called as rarely as | |
1436 | // possible! | |
c04ec496 VZ |
1437 | // |
1438 | // TODO: it can be implemented much more efficiently for GTK2 | |
c04ec496 | 1439 | #ifndef __WXGTK20__ |
22800f32 JS |
1440 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont, |
1441 | ||
01041145 | 1442 | _T("shouldn't be called for single line controls") ); |
22800f32 JS |
1443 | #else |
1444 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE), | |
1445 | _T("shouldn't be called for single line controls") ); | |
1446 | #endif | |
01041145 VZ |
1447 | |
1448 | wxString value = GetValue(); | |
1449 | if ( !value.IsEmpty() ) | |
1450 | { | |
c04ec496 | 1451 | SetUpdateFont(FALSE); |
572aeb77 | 1452 | |
01041145 VZ |
1453 | Clear(); |
1454 | AppendText(value); | |
01041145 VZ |
1455 | } |
1456 | } | |
1457 | ||
c04ec496 VZ |
1458 | #ifndef __WXGTK20__ |
1459 | ||
01041145 VZ |
1460 | void wxTextCtrl::UpdateFontIfNeeded() |
1461 | { | |
1462 | if ( m_updateFont ) | |
1463 | ChangeFontGlobally(); | |
1464 | } | |
1465 | ||
c04ec496 VZ |
1466 | #endif // GTK+ 1.x |
1467 | ||
17665a2b VZ |
1468 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) |
1469 | { | |
1470 | if ( !wxControl::SetForegroundColour(colour) ) | |
1471 | return FALSE; | |
1472 | ||
1473 | // update default fg colour too | |
1474 | m_defaultStyle.SetTextColour(colour); | |
1475 | ||
1476 | return TRUE; | |
1477 | } | |
1478 | ||
f03fc89f | 1479 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
68dda785 | 1480 | { |
223d09f6 | 1481 | wxCHECK_MSG( m_text != NULL, FALSE, wxT("invalid text ctrl") ); |
a81258be | 1482 | |
1f477433 VZ |
1483 | if ( !wxControl::SetBackgroundColour( colour ) ) |
1484 | return FALSE; | |
3358d36e | 1485 | |
c2094564 | 1486 | #ifndef __WXGTK20__ |
f03fc89f VZ |
1487 | if (!m_widget->window) |
1488 | return FALSE; | |
c2094564 | 1489 | #endif |
8bbe427f | 1490 | |
f03fc89f VZ |
1491 | if (!m_backgroundColour.Ok()) |
1492 | return FALSE; | |
8bbe427f | 1493 | |
ae0bdb01 RR |
1494 | if (m_windowStyle & wxTE_MULTILINE) |
1495 | { | |
1fc32b12 | 1496 | #ifndef __WXGTK20__ |
ae0bdb01 | 1497 | GdkWindow *window = GTK_TEXT(m_text)->text_area; |
f03fc89f VZ |
1498 | if (!window) |
1499 | return FALSE; | |
ae0bdb01 RR |
1500 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); |
1501 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
1502 | gdk_window_clear( window ); | |
fab591c5 | 1503 | #endif |
ae0bdb01 | 1504 | } |
f03fc89f | 1505 | |
17665a2b VZ |
1506 | // change active background color too |
1507 | m_defaultStyle.SetBackgroundColour( colour ); | |
1508 | ||
f03fc89f | 1509 | return TRUE; |
58614078 RR |
1510 | } |
1511 | ||
eda40bfc | 1512 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) |
17665a2b | 1513 | { |
17665a2b VZ |
1514 | if ( m_windowStyle & wxTE_MULTILINE ) |
1515 | { | |
1516 | if ( style.IsDefault() ) | |
1517 | { | |
1518 | // nothing to do | |
1519 | return TRUE; | |
1520 | } | |
cc3da3f8 RR |
1521 | #ifdef __WXGTK20__ |
1522 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(m_text) ); | |
1523 | gint l = gtk_text_buffer_get_char_count( text_buffer ); | |
17665a2b | 1524 | |
cc3da3f8 RR |
1525 | wxCHECK_MSG( start >= 0 && end <= l, FALSE, |
1526 | _T("invalid range in wxTextCtrl::SetStyle") ); | |
1527 | ||
1528 | GtkTextIter starti, endi; | |
1529 | gtk_text_buffer_get_iter_at_offset( text_buffer, &starti, start ); | |
1530 | gtk_text_buffer_get_iter_at_offset( text_buffer, &endi, end ); | |
1531 | ||
1532 | // use the attributes from style which are set in it and fall back | |
1533 | // first to the default style and then to the text control default | |
1534 | // colours for the others | |
1535 | wxTextAttr attr = wxTextAttr::Combine(style, m_defaultStyle, this); | |
1536 | ||
1537 | PangoFontDescription *font_description = attr.HasFont() | |
1538 | ? attr.GetFont().GetNativeFontInfo()->description | |
1539 | : NULL; | |
1540 | ||
1541 | GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor() | |
1542 | : NULL; | |
1543 | ||
1544 | GdkColor *colBg = attr.HasBackgroundColour() | |
1545 | ? attr.GetBackgroundColour().GetColor() | |
1546 | : NULL; | |
1547 | ||
1548 | GtkTextTag *tag; | |
1549 | tag = gtk_text_buffer_create_tag( text_buffer, NULL, "font-desc", font_description, | |
1550 | "foreground-gdk", colFg, | |
1551 | "background-gdk", colBg, NULL ); | |
1552 | gtk_text_buffer_apply_tag( text_buffer, tag, &starti, &endi ); | |
1553 | ||
1554 | return TRUE; | |
1555 | #else | |
1556 | // VERY dirty way to do that - removes the required text and re-adds it | |
1557 | // with styling (FIXME) | |
5b87f8bf | 1558 | |
17665a2b VZ |
1559 | gint l = gtk_text_get_length( GTK_TEXT(m_text) ); |
1560 | ||
1561 | wxCHECK_MSG( start >= 0 && end <= l, FALSE, | |
1562 | _T("invalid range in wxTextCtrl::SetStyle") ); | |
1563 | ||
1564 | gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) ); | |
1565 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end ); | |
1566 | wxString tmp(text,*wxConvCurrent); | |
1567 | g_free( text ); | |
1568 | ||
1569 | gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end ); | |
1570 | gtk_editable_set_position( GTK_EDITABLE(m_text), start ); | |
1571 | ||
1572 | #if wxUSE_UNICODE | |
1573 | wxWX2MBbuf buf = tmp.mbc_str(); | |
1574 | const char *txt = buf; | |
1575 | size_t txtlen = strlen(buf); | |
1576 | #else | |
1577 | const char *txt = tmp; | |
1578 | size_t txtlen = tmp.length(); | |
1579 | #endif | |
1580 | ||
eda40bfc VZ |
1581 | // use the attributes from style which are set in it and fall back |
1582 | // first to the default style and then to the text control default | |
1583 | // colours for the others | |
1584 | wxGtkTextInsert(m_text, | |
1585 | wxTextAttr::Combine(style, m_defaultStyle, this), | |
1586 | txt, | |
1587 | txtlen); | |
17665a2b VZ |
1588 | |
1589 | /* does not seem to help under GTK+ 1.2 !!! | |
1590 | gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */ | |
1591 | SetInsertionPoint( old_pos ); | |
fab591c5 | 1592 | #endif |
17665a2b VZ |
1593 | return TRUE; |
1594 | } | |
1595 | else // singe line | |
1596 | { | |
1597 | // cannot do this for GTK+'s Entry widget | |
1598 | return FALSE; | |
1599 | } | |
1600 | } | |
1601 | ||
f40fdaa3 | 1602 | void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) |
58614078 | 1603 | { |
f40fdaa3 | 1604 | gtk_widget_modify_style(m_text, style); |
68dda785 | 1605 | } |
f96aa4d9 | 1606 | |
e702ff0f JS |
1607 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
1608 | { | |
1609 | Cut(); | |
1610 | } | |
1611 | ||
1612 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1613 | { | |
1614 | Copy(); | |
1615 | } | |
1616 | ||
1617 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1618 | { | |
1619 | Paste(); | |
1620 | } | |
1621 | ||
1622 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1623 | { | |
1624 | Undo(); | |
1625 | } | |
1626 | ||
1627 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1628 | { | |
1629 | Redo(); | |
1630 | } | |
1631 | ||
1632 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1633 | { | |
1634 | event.Enable( CanCut() ); | |
1635 | } | |
1636 | ||
1637 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1638 | { | |
1639 | event.Enable( CanCopy() ); | |
1640 | } | |
1641 | ||
1642 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1643 | { | |
1644 | event.Enable( CanPaste() ); | |
1645 | } | |
1646 | ||
1647 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1648 | { | |
1649 | event.Enable( CanUndo() ); | |
1650 | } | |
1651 | ||
1652 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1653 | { | |
1654 | event.Enable( CanRedo() ); | |
1655 | } | |
65045edd RR |
1656 | |
1657 | void wxTextCtrl::OnInternalIdle() | |
1658 | { | |
1659 | wxCursor cursor = m_cursor; | |
1660 | if (g_globalCursor.Ok()) cursor = g_globalCursor; | |
1661 | ||
307f16e8 | 1662 | if (cursor.Ok()) |
65045edd | 1663 | { |
fab591c5 | 1664 | #ifndef __WXGTK20__ |
65045edd | 1665 | GdkWindow *window = (GdkWindow*) NULL; |
13111b2a | 1666 | if (HasFlag(wxTE_MULTILINE)) |
65045edd RR |
1667 | window = GTK_TEXT(m_text)->text_area; |
1668 | else | |
1669 | window = GTK_ENTRY(m_text)->text_area; | |
13111b2a | 1670 | |
65045edd RR |
1671 | if (window) |
1672 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
1673 | ||
1674 | if (!g_globalCursor.Ok()) | |
1675 | cursor = *wxSTANDARD_CURSOR; | |
1676 | ||
1677 | window = m_widget->window; | |
247e5b16 | 1678 | if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget))) |
65045edd | 1679 | gdk_window_set_cursor( window, cursor.GetCursor() ); |
fab591c5 | 1680 | #endif |
65045edd | 1681 | } |
26bf1ce0 | 1682 | |
d7fa7eaa RR |
1683 | if (g_delayedFocus == this) |
1684 | { | |
1685 | if (GTK_WIDGET_REALIZED(m_widget)) | |
1686 | { | |
1687 | gtk_widget_grab_focus( m_widget ); | |
1688 | g_delayedFocus = NULL; | |
1689 | } | |
1690 | } | |
1691 | ||
e39af974 JS |
1692 | if (wxUpdateUIEvent::CanUpdate(this)) |
1693 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
65045edd | 1694 | } |
f68586e5 VZ |
1695 | |
1696 | wxSize wxTextCtrl::DoGetBestSize() const | |
1697 | { | |
1698 | // FIXME should be different for multi-line controls... | |
0279e844 | 1699 | wxSize ret( wxControl::DoGetBestSize() ); |
9f884528 RD |
1700 | wxSize best(80, ret.y); |
1701 | CacheBestSize(best); | |
1702 | return best; | |
f68586e5 | 1703 | } |
0cc7251e | 1704 | |
9cd6d737 VZ |
1705 | // ---------------------------------------------------------------------------- |
1706 | // freeze/thaw | |
1707 | // ---------------------------------------------------------------------------- | |
1708 | ||
0cc7251e VZ |
1709 | void wxTextCtrl::Freeze() |
1710 | { | |
fab591c5 | 1711 | #ifndef __WXGTK20__ |
0cc7251e VZ |
1712 | if ( HasFlag(wxTE_MULTILINE) ) |
1713 | { | |
1714 | gtk_text_freeze(GTK_TEXT(m_text)); | |
1715 | } | |
fab591c5 | 1716 | #endif |
0cc7251e VZ |
1717 | } |
1718 | ||
1719 | void wxTextCtrl::Thaw() | |
1720 | { | |
fab591c5 | 1721 | #ifndef __WXGTK20__ |
0cc7251e VZ |
1722 | if ( HasFlag(wxTE_MULTILINE) ) |
1723 | { | |
817ec43a VZ |
1724 | GTK_TEXT(m_text)->vadj->value = 0.0; |
1725 | ||
0cc7251e VZ |
1726 | gtk_text_thaw(GTK_TEXT(m_text)); |
1727 | } | |
fab591c5 | 1728 | #endif |
0cc7251e | 1729 | } |
9cd6d737 VZ |
1730 | |
1731 | // ---------------------------------------------------------------------------- | |
1732 | // scrolling | |
1733 | // ---------------------------------------------------------------------------- | |
1734 | ||
1735 | GtkAdjustment *wxTextCtrl::GetVAdj() const | |
1736 | { | |
1ce52aa6 VZ |
1737 | if ( !IsMultiLine() ) |
1738 | return NULL; | |
1739 | ||
fab591c5 | 1740 | #ifdef __WXGTK20__ |
1ce52aa6 | 1741 | return gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_widget)); |
fab591c5 | 1742 | #else |
1ce52aa6 | 1743 | return GTK_TEXT(m_text)->vadj; |
fab591c5 | 1744 | #endif |
9cd6d737 VZ |
1745 | } |
1746 | ||
1747 | bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff) | |
1748 | { | |
1749 | float value = adj->value + diff; | |
1750 | ||
1751 | if ( value < 0 ) | |
1752 | value = 0; | |
1753 | ||
1754 | float upper = adj->upper - adj->page_size; | |
1755 | if ( value > upper ) | |
1756 | value = upper; | |
1757 | ||
1758 | // did we noticeably change the scroll position? | |
1759 | if ( fabs(adj->value - value) < 0.2 ) | |
1760 | { | |
1761 | // well, this is what Robert does in wxScrollBar, so it must be good... | |
1762 | return FALSE; | |
1763 | } | |
1764 | ||
1765 | adj->value = value; | |
1766 | ||
1ce52aa6 VZ |
1767 | #ifdef __WXGTK20__ |
1768 | gtk_adjustment_value_changed(GTK_ADJUSTMENT(adj)); | |
1769 | #else | |
9cd6d737 | 1770 | gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed"); |
fab591c5 | 1771 | #endif |
1ce52aa6 | 1772 | |
9cd6d737 VZ |
1773 | return TRUE; |
1774 | } | |
1775 | ||
1776 | bool wxTextCtrl::ScrollLines(int lines) | |
1777 | { | |
1778 | GtkAdjustment *adj = GetVAdj(); | |
1779 | if ( !adj ) | |
1780 | return FALSE; | |
1781 | ||
1ce52aa6 VZ |
1782 | #ifdef __WXGTK20__ |
1783 | int diff = (int)ceil(lines*adj->step_increment); | |
1784 | #else | |
9cd6d737 | 1785 | // this is hardcoded to 10 in GTK+ 1.2 (great idea) |
1ce52aa6 | 1786 | int diff = 10*lines; |
fab591c5 | 1787 | #endif |
1ce52aa6 VZ |
1788 | |
1789 | return DoScroll(adj, diff); | |
9cd6d737 VZ |
1790 | } |
1791 | ||
1792 | bool wxTextCtrl::ScrollPages(int pages) | |
1793 | { | |
1794 | GtkAdjustment *adj = GetVAdj(); | |
1795 | if ( !adj ) | |
1796 | return FALSE; | |
1797 | ||
817ec43a | 1798 | return DoScroll(adj, (int)ceil(pages*adj->page_increment)); |
9cd6d737 VZ |
1799 | } |
1800 | ||
9d522606 RD |
1801 | |
1802 | // static | |
1803 | wxVisualAttributes | |
1804 | wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1805 | { | |
1806 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
1807 | } |