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