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