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