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