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