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