]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
8e13c1ec | 2 | // Name: src/gtk1/textctrl.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin |
65571936 | 6 | // Licence: wxWindows licence |
c801d85f KB |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
14f355c2 VS |
9 | // For compilers that support precompilation, includes "wx.h". |
10 | #include "wx/wxprec.h" | |
11 | ||
c801d85f | 12 | #include "wx/textctrl.h" |
88a7a4e1 WS |
13 | |
14 | #ifndef WX_PRECOMP | |
844cada1 | 15 | #include "wx/app.h" |
88a7a4e1 | 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()) |
d270fef3 | 26 | #include "wx/evtloop.h" |
c801d85f | 27 | |
a81258be RR |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | |
30 | #include <ctype.h> | |
31 | ||
3cbab641 | 32 | #include "wx/gtk1/private.h" |
9e691f46 | 33 | #include <gdk/gdkkeysyms.h> |
b292e2f5 | 34 | |
acfd422a RR |
35 | //----------------------------------------------------------------------------- |
36 | // idle system | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | extern void wxapp_install_idle_handler(); | |
40 | extern bool g_isIdle; | |
41 | ||
b292e2f5 RR |
42 | //----------------------------------------------------------------------------- |
43 | // data | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
65045edd | 46 | extern wxCursor g_globalCursor; |
d7fa7eaa | 47 | extern wxWindowGTK *g_delayedFocus; |
b292e2f5 | 48 | |
eda40bfc VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // helpers | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
418cf02e | 53 | extern "C" { |
eda40bfc VZ |
54 | static void wxGtkTextInsert(GtkWidget *text, |
55 | const wxTextAttr& attr, | |
56 | const char *txt, | |
57 | size_t len) | |
58 | { | |
44cc96a8 JS |
59 | wxFont tmpFont; |
60 | GdkFont *font; | |
61 | if (attr.HasFont()) | |
62 | { | |
63 | tmpFont = attr.GetFont(); | |
64 | ||
65 | // FIXME: if this crashes because tmpFont goes out of scope and the GdkFont is | |
66 | // deleted, then we need to call gdk_font_ref on font. | |
67 | // This is because attr.GetFont() now returns a temporary font since wxTextAttr | |
68 | // no longer stores a wxFont object, for efficiency. | |
69 | ||
70 | font = tmpFont.GetInternalFont(); | |
71 | } | |
72 | else | |
73 | font = NULL; | |
eda40bfc VZ |
74 | |
75 | GdkColor *colFg = attr.HasTextColour() ? attr.GetTextColour().GetColor() | |
76 | : NULL; | |
77 | ||
78 | GdkColor *colBg = attr.HasBackgroundColour() | |
79 | ? attr.GetBackgroundColour().GetColor() | |
80 | : NULL; | |
81 | ||
82 | gtk_text_insert( GTK_TEXT(text), font, colFg, colBg, txt, len ); | |
83 | } | |
418cf02e | 84 | } |
eda40bfc | 85 | |
ce2f50e3 VZ |
86 | // ---------------------------------------------------------------------------- |
87 | // "insert_text" for GtkEntry | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
865bb325 | 90 | extern "C" { |
ce2f50e3 VZ |
91 | static void |
92 | gtk_insert_text_callback(GtkEditable *editable, | |
89954433 VZ |
93 | const gchar *WXUNUSED(new_text), |
94 | gint WXUNUSED(new_text_length), | |
95 | gint *WXUNUSED(position), | |
ce2f50e3 VZ |
96 | wxTextCtrl *win) |
97 | { | |
76fcf0f2 RR |
98 | if (g_isIdle) |
99 | wxapp_install_idle_handler(); | |
100 | ||
ce2f50e3 VZ |
101 | // we should only be called if we have a max len limit at all |
102 | GtkEntry *entry = GTK_ENTRY (editable); | |
103 | ||
9a83f860 | 104 | wxCHECK_RET( entry->text_max_length, wxT("shouldn't be called") ); |
ce2f50e3 VZ |
105 | |
106 | // check that we don't overflow the max length limit | |
107 | // | |
108 | // FIXME: this doesn't work when we paste a string which is going to be | |
109 | // truncated | |
110 | if ( entry->text_length == entry->text_max_length ) | |
111 | { | |
112 | // we don't need to run the base class version at all | |
113 | gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text"); | |
114 | ||
115 | // remember that the next changed signal is to be ignored to avoid | |
ce7fe42e | 116 | // generating a dummy wxEVT_TEXT event |
ce2f50e3 VZ |
117 | win->IgnoreNextTextUpdate(); |
118 | ||
119 | // and generate the correct one ourselves | |
ce7fe42e | 120 | wxCommandEvent event(wxEVT_TEXT_MAXLEN, win->GetId()); |
ce2f50e3 VZ |
121 | event.SetEventObject(win); |
122 | event.SetString(win->GetValue()); | |
937013e0 | 123 | win->HandleWindowEvent( event ); |
ce2f50e3 VZ |
124 | } |
125 | } | |
865bb325 | 126 | } |
ce2f50e3 | 127 | |
c801d85f | 128 | //----------------------------------------------------------------------------- |
2f2aa628 | 129 | // "changed" |
c801d85f KB |
130 | //----------------------------------------------------------------------------- |
131 | ||
865bb325 | 132 | extern "C" { |
805dd538 | 133 | static void |
89954433 | 134 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
484e45bf | 135 | { |
ce2f50e3 VZ |
136 | if ( win->IgnoreTextUpdate() ) |
137 | return; | |
138 | ||
a2053b27 | 139 | if (!win->m_hasVMT) return; |
805dd538 | 140 | |
bb69661b | 141 | if (g_isIdle) |
3c679789 | 142 | wxapp_install_idle_handler(); |
a8bf1826 | 143 | |
034be888 | 144 | win->SetModified(); |
01041145 | 145 | win->UpdateFontIfNeeded(); |
a8bf1826 | 146 | |
ce7fe42e | 147 | wxCommandEvent event( wxEVT_TEXT, win->GetId() ); |
2830bf19 | 148 | event.SetEventObject( win ); |
937013e0 | 149 | win->HandleWindowEvent( event ); |
6de97a3b | 150 | } |
865bb325 | 151 | } |
112892b9 | 152 | |
2830bf19 | 153 | //----------------------------------------------------------------------------- |
034be888 | 154 | // "changed" from vertical scrollbar |
2830bf19 RR |
155 | //----------------------------------------------------------------------------- |
156 | ||
865bb325 | 157 | extern "C" { |
805dd538 | 158 | static void |
034be888 | 159 | gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
2830bf19 | 160 | { |
3c679789 | 161 | if (!win->m_hasVMT) return; |
bb69661b VZ |
162 | |
163 | if (g_isIdle) | |
3c679789 | 164 | wxapp_install_idle_handler(); |
acfd422a | 165 | |
2830bf19 RR |
166 | win->CalculateScrollbar(); |
167 | } | |
865bb325 | 168 | } |
2830bf19 | 169 | |
1c35b54e VZ |
170 | // ---------------------------------------------------------------------------- |
171 | // redraw callback for multiline text | |
172 | // ---------------------------------------------------------------------------- | |
173 | ||
1c35b54e VZ |
174 | // redrawing a GtkText from inside a wxYield() call results in crashes (the |
175 | // text sample shows it in its "Add lines" command which shows wxProgressDialog | |
176 | // which implicitly calls wxYield()) so we override GtkText::draw() and simply | |
177 | // don't do anything if we're inside wxYield() | |
178 | ||
2e08b1a3 VZ |
179 | extern "C" { |
180 | typedef void (*GtkDrawCallback)(GtkWidget *widget, GdkRectangle *rect); | |
181 | } | |
1c35b54e VZ |
182 | |
183 | static GtkDrawCallback gs_gtk_text_draw = NULL; | |
184 | ||
865bb325 VZ |
185 | extern "C" { |
186 | static void wxgtk_text_draw( GtkWidget *widget, GdkRectangle *rect) | |
1c35b54e | 187 | { |
d270fef3 | 188 | wxEventLoopBase* loop = wxEventLoopBase::GetActive(); |
5b593995 | 189 | if ( loop && !loop->IsYielding() ) |
1c35b54e VZ |
190 | { |
191 | wxCHECK_RET( gs_gtk_text_draw != wxgtk_text_draw, | |
9a83f860 | 192 | wxT("infinite recursion in wxgtk_text_draw aborted") ); |
1c35b54e VZ |
193 | |
194 | gs_gtk_text_draw(widget, rect); | |
195 | } | |
196 | } | |
865bb325 | 197 | } |
1c35b54e | 198 | |
2f2aa628 RR |
199 | //----------------------------------------------------------------------------- |
200 | // wxTextCtrl | |
201 | //----------------------------------------------------------------------------- | |
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 | 224 | m_text = |
d3b9f782 | 225 | m_vScrollbar = NULL; |
9440c3d0 KH |
226 | } |
227 | ||
a0e55920 | 228 | wxTextCtrl::~wxTextCtrl() |
9440c3d0 | 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 ... |
d3b9f782 | 273 | m_text = gtk_text_new( NULL, NULL ); |
2830bf19 | 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 | |
55410bb4 | 406 | wxString wxTextCtrl::DoGetValue() 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 | ||
3a9fa0d6 VZ |
716 | void wxTextCtrl::MarkDirty() |
717 | { | |
7d8268a1 | 718 | m_modified = true; |
3a9fa0d6 VZ |
719 | } |
720 | ||
0efe5ba7 VZ |
721 | void wxTextCtrl::DiscardEdits() |
722 | { | |
7d8268a1 | 723 | m_modified = false; |
0efe5ba7 VZ |
724 | } |
725 | ||
ce2f50e3 VZ |
726 | // ---------------------------------------------------------------------------- |
727 | // max text length support | |
728 | // ---------------------------------------------------------------------------- | |
729 | ||
730 | void wxTextCtrl::IgnoreNextTextUpdate() | |
731 | { | |
7d8268a1 | 732 | m_ignoreNextUpdate = true; |
ce2f50e3 VZ |
733 | } |
734 | ||
735 | bool wxTextCtrl::IgnoreTextUpdate() | |
736 | { | |
737 | if ( m_ignoreNextUpdate ) | |
738 | { | |
7d8268a1 | 739 | m_ignoreNextUpdate = false; |
ce2f50e3 | 740 | |
7d8268a1 | 741 | return true; |
ce2f50e3 VZ |
742 | } |
743 | ||
7d8268a1 | 744 | return false; |
ce2f50e3 VZ |
745 | } |
746 | ||
d7eee191 VZ |
747 | void wxTextCtrl::SetMaxLength(unsigned long len) |
748 | { | |
749 | if ( !HasFlag(wxTE_MULTILINE) ) | |
750 | { | |
751 | gtk_entry_set_max_length(GTK_ENTRY(m_text), len); | |
ce2f50e3 VZ |
752 | |
753 | // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if | |
754 | // we had tried to enter more text than allowed by max text length and | |
755 | // the text wasn't really changed | |
756 | // | |
757 | // to detect this and generate TEXT_MAXLEN event instead of | |
758 | // TEXT_CHANGED one in this case we also catch "insert_text" signal | |
759 | // | |
760 | // when max len is set to 0 we disconnect our handler as it means that | |
761 | // we shouldn't check anything any more | |
762 | if ( len ) | |
763 | { | |
764 | gtk_signal_connect( GTK_OBJECT(m_text), | |
765 | "insert_text", | |
766 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
767 | (gpointer)this); | |
768 | } | |
769 | else // no checking | |
770 | { | |
771 | gtk_signal_disconnect_by_func | |
772 | ( | |
773 | GTK_OBJECT(m_text), | |
774 | GTK_SIGNAL_FUNC(gtk_insert_text_callback), | |
775 | (gpointer)this | |
776 | ); | |
777 | } | |
d7eee191 VZ |
778 | } |
779 | } | |
780 | ||
debe6624 | 781 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 782 | { |
223d09f6 | 783 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 784 | |
2b5f62a0 VZ |
785 | if (from == -1 && to == -1) |
786 | { | |
787 | from = 0; | |
8e13c1ec | 788 | to = GetValue().length(); |
2b5f62a0 VZ |
789 | } |
790 | ||
a1f79c1e VZ |
791 | if ( (m_windowStyle & wxTE_MULTILINE) && |
792 | !GTK_TEXT(m_text)->line_start_cache ) | |
793 | { | |
794 | // tell the programmer that it didn't work | |
9a83f860 | 795 | wxLogDebug(wxT("Can't call SetSelection() before realizing the control")); |
a1f79c1e VZ |
796 | return; |
797 | } | |
798 | ||
fab591c5 RR |
799 | if (m_windowStyle & wxTE_MULTILINE) |
800 | { | |
fab591c5 | 801 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
fab591c5 RR |
802 | } |
803 | else | |
804 | { | |
805 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); | |
806 | } | |
6de97a3b | 807 | } |
c801d85f | 808 | |
afbe906a | 809 | void wxTextCtrl::ShowPosition( long pos ) |
c801d85f | 810 | { |
afbe906a RR |
811 | if (m_windowStyle & wxTE_MULTILINE) |
812 | { | |
813 | GtkAdjustment *vp = GTK_TEXT(m_text)->vadj; | |
814 | float totalLines = (float) GetNumberOfLines(); | |
815 | long posX; | |
816 | long posY; | |
817 | PositionToXY(pos, &posX, &posY); | |
818 | float posLine = (float) posY; | |
819 | float p = (posLine/totalLines)*(vp->upper - vp->lower) + vp->lower; | |
820 | gtk_adjustment_set_value(GTK_TEXT(m_text)->vadj, p); | |
692c9b86 | 821 | } |
692c9b86 VZ |
822 | } |
823 | ||
03f38c58 | 824 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 825 | { |
223d09f6 | 826 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
3cbab641 | 827 | return (long) GET_EDITABLE_POS(m_text); |
6de97a3b | 828 | } |
c801d85f | 829 | |
7d8268a1 | 830 | wxTextPos wxTextCtrl::GetLastPosition() const |
c801d85f | 831 | { |
223d09f6 | 832 | wxCHECK_MSG( m_text != NULL, 0, wxT("invalid text ctrl") ); |
8bbe427f | 833 | |
2830bf19 | 834 | int pos = 0; |
a8bf1826 | 835 | |
2830bf19 | 836 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 | 837 | { |
2830bf19 | 838 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); |
fab591c5 | 839 | } |
2830bf19 | 840 | else |
fab591c5 | 841 | { |
2830bf19 | 842 | pos = GTK_ENTRY(m_text)->text_length; |
fab591c5 | 843 | } |
805dd538 | 844 | |
ac0d36b5 | 845 | return (long)pos; |
6de97a3b | 846 | } |
c801d85f | 847 | |
debe6624 | 848 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 849 | { |
223d09f6 | 850 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
581ee8a9 | 851 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 852 | } |
c801d85f | 853 | |
debe6624 | 854 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 855 | { |
223d09f6 | 856 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
8bbe427f | 857 | |
581ee8a9 | 858 | Remove( from, to ); |
bb69661b | 859 | |
7d8268a1 | 860 | if (!value.empty()) |
2df7be7f RR |
861 | { |
862 | gint pos = (gint)from; | |
05939a81 | 863 | #if wxUSE_UNICODE |
2df7be7f RR |
864 | wxWX2MBbuf buf = value.mbc_str(); |
865 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos ); | |
05939a81 | 866 | #else |
8e13c1ec | 867 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.length(), &pos ); |
581ee8a9 | 868 | #endif // wxUSE_UNICODE |
2df7be7f | 869 | } |
6de97a3b | 870 | } |
c801d85f | 871 | |
03f38c58 | 872 | void wxTextCtrl::Cut() |
c801d85f | 873 | { |
223d09f6 | 874 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3cbab641 | 875 | gtk_editable_cut_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 876 | } |
c801d85f | 877 | |
03f38c58 | 878 | void wxTextCtrl::Copy() |
c801d85f | 879 | { |
223d09f6 | 880 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3cbab641 | 881 | gtk_editable_copy_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 882 | } |
c801d85f | 883 | |
03f38c58 | 884 | void wxTextCtrl::Paste() |
c801d85f | 885 | { |
223d09f6 | 886 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
3cbab641 | 887 | gtk_editable_paste_clipboard(GTK_EDITABLE(m_text) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 888 | } |
c801d85f | 889 | |
ca8b28f2 JS |
890 | // Undo/redo |
891 | void wxTextCtrl::Undo() | |
892 | { | |
893 | // TODO | |
223d09f6 | 894 | wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") ); |
ca8b28f2 JS |
895 | } |
896 | ||
897 | void wxTextCtrl::Redo() | |
898 | { | |
899 | // TODO | |
223d09f6 | 900 | wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") ); |
ca8b28f2 JS |
901 | } |
902 | ||
903 | bool wxTextCtrl::CanUndo() const | |
904 | { | |
905 | // TODO | |
4855a477 | 906 | //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") ); |
7d8268a1 | 907 | return false; |
ca8b28f2 JS |
908 | } |
909 | ||
910 | bool wxTextCtrl::CanRedo() const | |
911 | { | |
912 | // TODO | |
4855a477 | 913 | //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") ); |
7d8268a1 | 914 | return false; |
ca8b28f2 JS |
915 | } |
916 | ||
917 | // If the return values from and to are the same, there is no | |
918 | // selection. | |
2d4cc5b6 | 919 | void wxTextCtrl::GetSelection(long* fromOut, long* toOut) const |
ca8b28f2 | 920 | { |
223d09f6 | 921 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
bb69661b | 922 | |
5b87f8bf RD |
923 | gint from = -1; |
924 | gint to = -1; | |
7d8268a1 | 925 | bool haveSelection = false; |
5b87f8bf | 926 | |
5b87f8bf RD |
927 | if ( (GTK_EDITABLE(m_text)->has_selection) ) |
928 | { | |
7d8268a1 | 929 | haveSelection = true; |
5b87f8bf RD |
930 | from = (long) GTK_EDITABLE(m_text)->selection_start_pos; |
931 | to = (long) GTK_EDITABLE(m_text)->selection_end_pos; | |
932 | } | |
2d4cc5b6 | 933 | |
5b87f8bf RD |
934 | if (! haveSelection ) |
935 | from = to = GetInsertionPoint(); | |
936 | ||
937 | if ( from > to ) | |
938 | { | |
939 | // exchange them to be compatible with wxMSW | |
940 | gint tmp = from; | |
941 | from = to; | |
942 | to = tmp; | |
943 | } | |
bb69661b | 944 | |
2d4cc5b6 VZ |
945 | if ( fromOut ) |
946 | *fromOut = from; | |
947 | if ( toOut ) | |
948 | *toOut = to; | |
ca8b28f2 JS |
949 | } |
950 | ||
5b87f8bf | 951 | |
ca8b28f2 JS |
952 | bool wxTextCtrl::IsEditable() const |
953 | { | |
7d8268a1 | 954 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
05060eeb | 955 | return GTK_EDITABLE(m_text)->editable; |
ca8b28f2 JS |
956 | } |
957 | ||
0efe5ba7 VZ |
958 | bool wxTextCtrl::IsModified() const |
959 | { | |
960 | return m_modified; | |
961 | } | |
962 | ||
03f38c58 | 963 | void wxTextCtrl::Clear() |
c801d85f | 964 | { |
902725ee | 965 | SetValue( wxEmptyString ); |
6de97a3b | 966 | } |
c801d85f | 967 | |
903f689b | 968 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 969 | { |
223d09f6 | 970 | wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") ); |
805dd538 | 971 | |
8e13c1ec | 972 | if ((key_event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER)) |
2830bf19 | 973 | { |
ce7fe42e | 974 | wxCommandEvent event(wxEVT_TEXT_ENTER, m_windowId); |
2830bf19 | 975 | event.SetEventObject(this); |
f6bcfd97 | 976 | event.SetString(GetValue()); |
937013e0 | 977 | if (HandleWindowEvent(event)) return; |
2830bf19 | 978 | } |
903f689b | 979 | |
12a3f227 | 980 | if ((key_event.GetKeyCode() == WXK_RETURN) && !(m_windowStyle & wxTE_MULTILINE)) |
da048e3d | 981 | { |
2b328fc9 RR |
982 | // This will invoke the dialog default action, such |
983 | // as the clicking the default button. | |
a8bf1826 | 984 | |
da048e3d | 985 | wxWindow *top_frame = m_parent; |
8487f887 | 986 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
da048e3d | 987 | top_frame = top_frame->GetParent(); |
a8bf1826 | 988 | |
2b328fc9 | 989 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) |
da048e3d | 990 | { |
2b328fc9 RR |
991 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
992 | ||
993 | if (window->default_widget) | |
994 | { | |
995 | gtk_widget_activate (window->default_widget); | |
996 | return; | |
997 | } | |
13111b2a | 998 | } |
da048e3d RR |
999 | } |
1000 | ||
2830bf19 | 1001 | key_event.Skip(); |
6de97a3b | 1002 | } |
c801d85f | 1003 | |
03f38c58 | 1004 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 1005 | { |
ae0bdb01 | 1006 | return GTK_WIDGET(m_text); |
6de97a3b | 1007 | } |
e3e65dac | 1008 | |
903f689b RR |
1009 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
1010 | { | |
ae0bdb01 | 1011 | if (m_windowStyle & wxTE_MULTILINE) |
fab591c5 | 1012 | { |
ae0bdb01 | 1013 | return (window == GTK_TEXT(m_text)->text_area); |
fab591c5 | 1014 | } |
ae0bdb01 | 1015 | else |
fab591c5 | 1016 | { |
ae0bdb01 | 1017 | return (window == GTK_ENTRY(m_text)->text_area); |
fab591c5 | 1018 | } |
903f689b | 1019 | } |
e3e65dac | 1020 | |
bb69661b VZ |
1021 | // the font will change for subsequent text insertiongs |
1022 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
868a2826 | 1023 | { |
7d8268a1 | 1024 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
8bbe427f | 1025 | |
a66954a6 | 1026 | if ( !wxTextCtrlBase::SetFont(font) ) |
bb69661b VZ |
1027 | { |
1028 | // font didn't change, nothing to do | |
7d8268a1 | 1029 | return false; |
bb69661b VZ |
1030 | } |
1031 | ||
1032 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1033 | { | |
7d8268a1 | 1034 | SetUpdateFont(true); |
bb69661b | 1035 | |
1ff4714d VZ |
1036 | m_defaultStyle.SetFont(font); |
1037 | ||
01041145 | 1038 | ChangeFontGlobally(); |
bb69661b VZ |
1039 | } |
1040 | ||
7d8268a1 | 1041 | return true; |
58614078 RR |
1042 | } |
1043 | ||
01041145 VZ |
1044 | void wxTextCtrl::ChangeFontGlobally() |
1045 | { | |
1046 | // this method is very inefficient and hence should be called as rarely as | |
1047 | // possible! | |
22800f32 JS |
1048 | wxASSERT_MSG( (m_windowStyle & wxTE_MULTILINE) && m_updateFont, |
1049 | ||
9a83f860 | 1050 | wxT("shouldn't be called for single line controls") ); |
01041145 VZ |
1051 | |
1052 | wxString value = GetValue(); | |
7d8268a1 | 1053 | if ( !value.empty() ) |
01041145 | 1054 | { |
7d8268a1 | 1055 | SetUpdateFont(false); |
572aeb77 | 1056 | |
01041145 VZ |
1057 | Clear(); |
1058 | AppendText(value); | |
01041145 VZ |
1059 | } |
1060 | } | |
1061 | ||
1062 | void wxTextCtrl::UpdateFontIfNeeded() | |
1063 | { | |
1064 | if ( m_updateFont ) | |
1065 | ChangeFontGlobally(); | |
1066 | } | |
1067 | ||
17665a2b VZ |
1068 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) |
1069 | { | |
1070 | if ( !wxControl::SetForegroundColour(colour) ) | |
7d8268a1 | 1071 | return false; |
17665a2b VZ |
1072 | |
1073 | // update default fg colour too | |
1074 | m_defaultStyle.SetTextColour(colour); | |
1075 | ||
7d8268a1 | 1076 | return true; |
17665a2b VZ |
1077 | } |
1078 | ||
f03fc89f | 1079 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
68dda785 | 1080 | { |
7d8268a1 | 1081 | wxCHECK_MSG( m_text != NULL, false, wxT("invalid text ctrl") ); |
a81258be | 1082 | |
1f477433 | 1083 | if ( !wxControl::SetBackgroundColour( colour ) ) |
7d8268a1 | 1084 | return false; |
3358d36e | 1085 | |
f03fc89f | 1086 | if (!m_widget->window) |
7d8268a1 | 1087 | return false; |
8bbe427f | 1088 | |
a1b806b9 | 1089 | if (!m_backgroundColour.IsOk()) |
7d8268a1 | 1090 | return false; |
8bbe427f | 1091 | |
ae0bdb01 RR |
1092 | if (m_windowStyle & wxTE_MULTILINE) |
1093 | { | |
1094 | GdkWindow *window = GTK_TEXT(m_text)->text_area; | |
f03fc89f | 1095 | if (!window) |
7d8268a1 | 1096 | return false; |
ae0bdb01 RR |
1097 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); |
1098 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
1099 | gdk_window_clear( window ); | |
1100 | } | |
f03fc89f | 1101 | |
17665a2b VZ |
1102 | // change active background color too |
1103 | m_defaultStyle.SetBackgroundColour( colour ); | |
1104 | ||
7d8268a1 | 1105 | return true; |
58614078 RR |
1106 | } |
1107 | ||
eda40bfc | 1108 | bool wxTextCtrl::SetStyle( long start, long end, const wxTextAttr& style ) |
17665a2b | 1109 | { |
17665a2b VZ |
1110 | if ( m_windowStyle & wxTE_MULTILINE ) |
1111 | { | |
1112 | if ( style.IsDefault() ) | |
1113 | { | |
1114 | // nothing to do | |
7d8268a1 | 1115 | return true; |
17665a2b | 1116 | } |
902725ee | 1117 | |
cc3da3f8 RR |
1118 | // VERY dirty way to do that - removes the required text and re-adds it |
1119 | // with styling (FIXME) | |
5b87f8bf | 1120 | |
17665a2b VZ |
1121 | gint l = gtk_text_get_length( GTK_TEXT(m_text) ); |
1122 | ||
7d8268a1 | 1123 | wxCHECK_MSG( start >= 0 && end <= l, false, |
9a83f860 | 1124 | wxT("invalid range in wxTextCtrl::SetStyle") ); |
17665a2b VZ |
1125 | |
1126 | gint old_pos = gtk_editable_get_position( GTK_EDITABLE(m_text) ); | |
1127 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), start, end ); | |
1128 | wxString tmp(text,*wxConvCurrent); | |
1129 | g_free( text ); | |
1130 | ||
1131 | gtk_editable_delete_text( GTK_EDITABLE(m_text), start, end ); | |
1132 | gtk_editable_set_position( GTK_EDITABLE(m_text), start ); | |
1133 | ||
902725ee | 1134 | #if wxUSE_UNICODE |
17665a2b VZ |
1135 | wxWX2MBbuf buf = tmp.mbc_str(); |
1136 | const char *txt = buf; | |
1137 | size_t txtlen = strlen(buf); | |
902725ee | 1138 | #else |
17665a2b VZ |
1139 | const char *txt = tmp; |
1140 | size_t txtlen = tmp.length(); | |
902725ee | 1141 | #endif |
17665a2b | 1142 | |
eda40bfc VZ |
1143 | // use the attributes from style which are set in it and fall back |
1144 | // first to the default style and then to the text control default | |
1145 | // colours for the others | |
1146 | wxGtkTextInsert(m_text, | |
1147 | wxTextAttr::Combine(style, m_defaultStyle, this), | |
1148 | txt, | |
1149 | txtlen); | |
17665a2b VZ |
1150 | |
1151 | /* does not seem to help under GTK+ 1.2 !!! | |
1152 | gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */ | |
1153 | SetInsertionPoint( old_pos ); | |
902725ee | 1154 | |
7d8268a1 | 1155 | return true; |
17665a2b | 1156 | } |
902725ee WS |
1157 | |
1158 | // else single line | |
1159 | // cannot do this for GTK+'s Entry widget | |
1160 | return false; | |
17665a2b VZ |
1161 | } |
1162 | ||
f40fdaa3 | 1163 | void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle *style) |
58614078 | 1164 | { |
f40fdaa3 | 1165 | gtk_widget_modify_style(m_text, style); |
68dda785 | 1166 | } |
f96aa4d9 | 1167 | |
e702ff0f JS |
1168 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
1169 | { | |
1170 | Cut(); | |
1171 | } | |
1172 | ||
1173 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1174 | { | |
1175 | Copy(); | |
1176 | } | |
1177 | ||
1178 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1179 | { | |
1180 | Paste(); | |
1181 | } | |
1182 | ||
1183 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
1184 | { | |
1185 | Undo(); | |
1186 | } | |
1187 | ||
1188 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
1189 | { | |
1190 | Redo(); | |
1191 | } | |
1192 | ||
1193 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1194 | { | |
1195 | event.Enable( CanCut() ); | |
1196 | } | |
1197 | ||
1198 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1199 | { | |
1200 | event.Enable( CanCopy() ); | |
1201 | } | |
1202 | ||
1203 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1204 | { | |
1205 | event.Enable( CanPaste() ); | |
1206 | } | |
1207 | ||
1208 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1209 | { | |
1210 | event.Enable( CanUndo() ); | |
1211 | } | |
1212 | ||
1213 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1214 | { | |
1215 | event.Enable( CanRedo() ); | |
1216 | } | |
65045edd RR |
1217 | |
1218 | void wxTextCtrl::OnInternalIdle() | |
1219 | { | |
1220 | wxCursor cursor = m_cursor; | |
a1b806b9 | 1221 | if (g_globalCursor.IsOk()) cursor = g_globalCursor; |
65045edd | 1222 | |
a1b806b9 | 1223 | if (cursor.IsOk()) |
65045edd | 1224 | { |
d3b9f782 | 1225 | GdkWindow *window = NULL; |
13111b2a | 1226 | if (HasFlag(wxTE_MULTILINE)) |
65045edd RR |
1227 | window = GTK_TEXT(m_text)->text_area; |
1228 | else | |
1229 | window = GTK_ENTRY(m_text)->text_area; | |
13111b2a | 1230 | |
65045edd RR |
1231 | if (window) |
1232 | gdk_window_set_cursor( window, cursor.GetCursor() ); | |
1233 | ||
a1b806b9 | 1234 | if (!g_globalCursor.IsOk()) |
65045edd RR |
1235 | cursor = *wxSTANDARD_CURSOR; |
1236 | ||
1237 | window = m_widget->window; | |
247e5b16 | 1238 | if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget))) |
65045edd RR |
1239 | gdk_window_set_cursor( window, cursor.GetCursor() ); |
1240 | } | |
26bf1ce0 | 1241 | |
d7fa7eaa RR |
1242 | if (g_delayedFocus == this) |
1243 | { | |
1244 | if (GTK_WIDGET_REALIZED(m_widget)) | |
1245 | { | |
1246 | gtk_widget_grab_focus( m_widget ); | |
1247 | g_delayedFocus = NULL; | |
1248 | } | |
1249 | } | |
1250 | ||
e39af974 JS |
1251 | if (wxUpdateUIEvent::CanUpdate(this)) |
1252 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
65045edd | 1253 | } |
f68586e5 VZ |
1254 | |
1255 | wxSize wxTextCtrl::DoGetBestSize() const | |
1256 | { | |
1257 | // FIXME should be different for multi-line controls... | |
0279e844 | 1258 | wxSize ret( wxControl::DoGetBestSize() ); |
9f884528 RD |
1259 | wxSize best(80, ret.y); |
1260 | CacheBestSize(best); | |
1261 | return best; | |
f68586e5 | 1262 | } |
0cc7251e | 1263 | |
9cd6d737 VZ |
1264 | // ---------------------------------------------------------------------------- |
1265 | // freeze/thaw | |
1266 | // ---------------------------------------------------------------------------- | |
1267 | ||
17808a75 | 1268 | void wxTextCtrl::DoFreeze() |
0cc7251e VZ |
1269 | { |
1270 | if ( HasFlag(wxTE_MULTILINE) ) | |
1271 | { | |
41b81aed | 1272 | gtk_text_freeze(GTK_TEXT(m_text)); |
41b81aed | 1273 | } |
0cc7251e VZ |
1274 | } |
1275 | ||
17808a75 | 1276 | void wxTextCtrl::DoThaw() |
0cc7251e VZ |
1277 | { |
1278 | if ( HasFlag(wxTE_MULTILINE) ) | |
1279 | { | |
817ec43a VZ |
1280 | GTK_TEXT(m_text)->vadj->value = 0.0; |
1281 | ||
0cc7251e | 1282 | gtk_text_thaw(GTK_TEXT(m_text)); |
9440c3d0 | 1283 | } |
9440c3d0 | 1284 | } |
9440c3d0 | 1285 | |
9cd6d737 VZ |
1286 | // ---------------------------------------------------------------------------- |
1287 | // scrolling | |
1288 | // ---------------------------------------------------------------------------- | |
1289 | ||
1290 | GtkAdjustment *wxTextCtrl::GetVAdj() const | |
1291 | { | |
1ce52aa6 VZ |
1292 | if ( !IsMultiLine() ) |
1293 | return NULL; | |
1294 | ||
1ce52aa6 | 1295 | return GTK_TEXT(m_text)->vadj; |
9cd6d737 VZ |
1296 | } |
1297 | ||
1298 | bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff) | |
1299 | { | |
1300 | float value = adj->value + diff; | |
1301 | ||
1302 | if ( value < 0 ) | |
1303 | value = 0; | |
1304 | ||
1305 | float upper = adj->upper - adj->page_size; | |
1306 | if ( value > upper ) | |
1307 | value = upper; | |
1308 | ||
1309 | // did we noticeably change the scroll position? | |
1310 | if ( fabs(adj->value - value) < 0.2 ) | |
1311 | { | |
1312 | // well, this is what Robert does in wxScrollBar, so it must be good... | |
7d8268a1 | 1313 | return false; |
9cd6d737 VZ |
1314 | } |
1315 | ||
1316 | adj->value = value; | |
9cd6d737 | 1317 | gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed"); |
1ce52aa6 | 1318 | |
7d8268a1 | 1319 | return true; |
9cd6d737 VZ |
1320 | } |
1321 | ||
1322 | bool wxTextCtrl::ScrollLines(int lines) | |
1323 | { | |
1324 | GtkAdjustment *adj = GetVAdj(); | |
1325 | if ( !adj ) | |
7d8268a1 | 1326 | return false; |
9cd6d737 VZ |
1327 | |
1328 | // this is hardcoded to 10 in GTK+ 1.2 (great idea) | |
1ce52aa6 | 1329 | int diff = 10*lines; |
1ce52aa6 VZ |
1330 | |
1331 | return DoScroll(adj, diff); | |
9cd6d737 VZ |
1332 | } |
1333 | ||
1334 | bool wxTextCtrl::ScrollPages(int pages) | |
1335 | { | |
1336 | GtkAdjustment *adj = GetVAdj(); | |
1337 | if ( !adj ) | |
7d8268a1 | 1338 | return false; |
9cd6d737 | 1339 | |
817ec43a | 1340 | return DoScroll(adj, (int)ceil(pages*adj->page_increment)); |
9cd6d737 VZ |
1341 | } |
1342 | ||
9d522606 RD |
1343 | |
1344 | // static | |
1345 | wxVisualAttributes | |
1346 | wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1347 | { | |
1348 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
1349 | } |