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