]>
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 RR |
16 | #include "wx/intl.h" |
17 | #include "wx/settings.h" | |
c801d85f | 18 | |
a81258be RR |
19 | #include <sys/types.h> |
20 | #include <sys/stat.h> | |
21 | #include <ctype.h> | |
22 | ||
83624f79 RR |
23 | #include "gdk/gdk.h" |
24 | #include "gtk/gtk.h" | |
b292e2f5 RR |
25 | #include "gdk/gdkkeysyms.h" |
26 | ||
acfd422a RR |
27 | //----------------------------------------------------------------------------- |
28 | // idle system | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | extern void wxapp_install_idle_handler(); | |
32 | extern bool g_isIdle; | |
33 | ||
b292e2f5 RR |
34 | //----------------------------------------------------------------------------- |
35 | // data | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern bool g_blockEventsOnDrag; | |
39 | ||
c801d85f | 40 | //----------------------------------------------------------------------------- |
2f2aa628 | 41 | // "changed" |
c801d85f KB |
42 | //----------------------------------------------------------------------------- |
43 | ||
805dd538 | 44 | static void |
2830bf19 | 45 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
484e45bf | 46 | { |
a2053b27 | 47 | if (!win->m_hasVMT) return; |
805dd538 | 48 | |
bb69661b | 49 | if (g_isIdle) |
3c679789 RR |
50 | wxapp_install_idle_handler(); |
51 | ||
034be888 | 52 | win->SetModified(); |
8bbe427f | 53 | |
f03fc89f | 54 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); |
8a85884a | 55 | event.SetString( win->GetValue() ); |
2830bf19 RR |
56 | event.SetEventObject( win ); |
57 | win->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 58 | } |
112892b9 | 59 | |
2830bf19 | 60 | //----------------------------------------------------------------------------- |
034be888 | 61 | // "changed" from vertical scrollbar |
2830bf19 RR |
62 | //----------------------------------------------------------------------------- |
63 | ||
805dd538 | 64 | static void |
034be888 | 65 | gtk_scrollbar_changed_callback( GtkWidget *WXUNUSED(widget), wxTextCtrl *win ) |
2830bf19 | 66 | { |
3c679789 | 67 | if (!win->m_hasVMT) return; |
bb69661b VZ |
68 | |
69 | if (g_isIdle) | |
3c679789 | 70 | wxapp_install_idle_handler(); |
acfd422a | 71 | |
2830bf19 RR |
72 | win->CalculateScrollbar(); |
73 | } | |
74 | ||
2f2aa628 RR |
75 | //----------------------------------------------------------------------------- |
76 | // wxTextCtrl | |
77 | //----------------------------------------------------------------------------- | |
78 | ||
79 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
80 | ||
c801d85f | 81 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
2830bf19 | 82 | EVT_CHAR(wxTextCtrl::OnChar) |
e702ff0f JS |
83 | |
84 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
85 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
86 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
87 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
88 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
89 | ||
90 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
91 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
92 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
93 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
94 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
c801d85f KB |
95 | END_EVENT_TABLE() |
96 | ||
f5abe911 RR |
97 | wxTextCtrl::wxTextCtrl() |
98 | { | |
99 | m_modified = FALSE; | |
100 | } | |
13289f04 | 101 | |
f5abe911 RR |
102 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, |
103 | const wxPoint &pos, const wxSize &size, | |
104 | int style, const wxValidator& validator, const wxString &name ) | |
105 | { | |
106 | m_modified = FALSE; | |
107 | Create( parent, id, value, pos, size, style, validator, name ); | |
108 | } | |
c801d85f | 109 | |
debe6624 | 110 | bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 111 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 112 | int style, const wxValidator& validator, const wxString &name ) |
c801d85f | 113 | { |
2830bf19 | 114 | m_needParent = TRUE; |
b292e2f5 | 115 | m_acceptsFocus = TRUE; |
484e45bf | 116 | |
2830bf19 | 117 | PreCreation( parent, id, pos, size, style, name ); |
6de97a3b | 118 | |
88ac883a | 119 | #if wxUSE_VALIDATORS |
2830bf19 | 120 | SetValidator( validator ); |
88ac883a | 121 | #endif // wxUSE_VALIDATORS |
805dd538 | 122 | |
034be888 | 123 | m_vScrollbarVisible = FALSE; |
13289f04 | 124 | |
2830bf19 | 125 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
ab46dc18 | 126 | if (multi_line) |
2830bf19 | 127 | { |
7d6d2cd4 | 128 | #if (GTK_MINOR_VERSION > 2) |
034be888 RR |
129 | /* a multi-line edit control: create a vertical scrollbar by default and |
130 | horizontal if requested */ | |
2830bf19 | 131 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; |
7d6d2cd4 RR |
132 | #else |
133 | bool bHasHScrollbar = FALSE; | |
134 | #endif | |
805dd538 | 135 | |
034be888 | 136 | /* create our control ... */ |
2830bf19 RR |
137 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); |
138 | ||
034be888 | 139 | /* ... and put into the upper left hand corner of the table */ |
2830bf19 | 140 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); |
5664fc32 | 141 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); |
2830bf19 | 142 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, |
41dee9d0 | 143 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
f5368809 RR |
144 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
145 | 0, 0); | |
bb69661b | 146 | |
5c387335 RR |
147 | /* always wrap words */ |
148 | gtk_text_set_word_wrap( GTK_TEXT(m_text), TRUE ); | |
bb69661b | 149 | |
7d6d2cd4 | 150 | #if (GTK_MINOR_VERSION > 2) |
034be888 | 151 | /* put the horizontal scrollbar in the lower left hand corner */ |
2830bf19 RR |
152 | if (bHasHScrollbar) |
153 | { | |
154 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); | |
5664fc32 | 155 | GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS ); |
2830bf19 | 156 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, |
8ce63e9d | 157 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), |
13289f04 VZ |
158 | GTK_FILL, |
159 | 0, 0); | |
2830bf19 | 160 | gtk_widget_show(hscrollbar); |
13289f04 | 161 | |
3358d36e VZ |
162 | /* don't wrap lines, otherwise we wouldn't need the scrollbar */ |
163 | gtk_text_set_line_wrap( GTK_TEXT(m_text), FALSE ); | |
5c387335 | 164 | } |
7d6d2cd4 | 165 | #endif |
bb69661b | 166 | |
ab46dc18 RR |
167 | /* finally, put the vertical scrollbar in the upper right corner */ |
168 | m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj ); | |
169 | GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS ); | |
ab46dc18 RR |
170 | gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1, |
171 | GTK_FILL, | |
172 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), | |
173 | 0, 0); | |
2830bf19 RR |
174 | } |
175 | else | |
176 | { | |
034be888 | 177 | /* a single-line text control: no need for scrollbars */ |
2830bf19 RR |
178 | m_widget = |
179 | m_text = gtk_entry_new(); | |
180 | } | |
484e45bf | 181 | |
2830bf19 RR |
182 | wxSize newSize = size; |
183 | if (newSize.x == -1) newSize.x = 80; | |
184 | if (newSize.y == -1) newSize.y = 26; | |
185 | SetSize( newSize.x, newSize.y ); | |
484e45bf | 186 | |
f03fc89f | 187 | m_parent->DoAddChild( this ); |
8bbe427f | 188 | |
2830bf19 | 189 | PostCreation(); |
484e45bf | 190 | |
2830bf19 | 191 | if (multi_line) |
2830bf19 | 192 | gtk_widget_show(m_text); |
13289f04 | 193 | |
034be888 | 194 | /* we want to be notified about text changes */ |
b292e2f5 RR |
195 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
196 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
484e45bf | 197 | |
ab46dc18 RR |
198 | if (multi_line) |
199 | { | |
200 | gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text)->vadj), "changed", | |
201 | (GtkSignalFunc) gtk_scrollbar_changed_callback, (gpointer) this ); | |
202 | } | |
3358d36e | 203 | |
291a8f20 | 204 | if (!value.IsEmpty()) |
2830bf19 RR |
205 | { |
206 | gint tmp = 0; | |
3358d36e VZ |
207 | |
208 | #if GTK_MINOR_VERSION == 0 | |
209 | // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in | |
210 | // gtk_editable_insert_text() | |
211 | gtk_widget_realize(m_text); | |
212 | #endif // GTK 1.0 | |
213 | ||
05939a81 | 214 | #if wxUSE_UNICODE |
3358d36e | 215 | wxWX2MBbuf val = value.mbc_str(); |
05939a81 | 216 | gtk_editable_insert_text( GTK_EDITABLE(m_text), val, strlen(val), &tmp ); |
3358d36e | 217 | #else // !Unicode |
2830bf19 | 218 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); |
3358d36e VZ |
219 | #endif // Unicode/!Unicode |
220 | ||
291a8f20 RR |
221 | if (multi_line) |
222 | { | |
3358d36e VZ |
223 | /* bring editable's cursor uptodate. bug in GTK. */ |
224 | ||
225 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
226 | } | |
2830bf19 | 227 | } |
484e45bf | 228 | |
2830bf19 RR |
229 | if (style & wxTE_PASSWORD) |
230 | { | |
231 | if (!multi_line) | |
232 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
233 | } | |
8bbe427f | 234 | |
2830bf19 RR |
235 | if (style & wxTE_READONLY) |
236 | { | |
237 | if (!multi_line) | |
238 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
239 | } | |
240 | else | |
241 | { | |
242 | if (multi_line) | |
243 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
244 | } | |
3358d36e | 245 | |
012a03e0 | 246 | SetBackgroundColour( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW) ); |
034be888 | 247 | SetForegroundColour( parent->GetForegroundColour() ); |
805dd538 | 248 | |
2830bf19 | 249 | Show( TRUE ); |
805dd538 | 250 | |
2830bf19 RR |
251 | return TRUE; |
252 | } | |
484e45bf | 253 | |
2830bf19 RR |
254 | void wxTextCtrl::CalculateScrollbar() |
255 | { | |
256 | if ((m_windowStyle & wxTE_MULTILINE) == 0) return; | |
f96aa4d9 | 257 | |
2830bf19 | 258 | GtkAdjustment *adj = GTK_TEXT(m_text)->vadj; |
805dd538 | 259 | |
2830bf19 RR |
260 | if (adj->upper - adj->page_size < 0.8) |
261 | { | |
262 | if (m_vScrollbarVisible) | |
263 | { | |
034be888 | 264 | gtk_widget_hide( m_vScrollbar ); |
034be888 | 265 | m_vScrollbarVisible = FALSE; |
2830bf19 RR |
266 | } |
267 | } | |
268 | else | |
269 | { | |
270 | if (!m_vScrollbarVisible) | |
271 | { | |
034be888 | 272 | gtk_widget_show( m_vScrollbar ); |
034be888 | 273 | m_vScrollbarVisible = TRUE; |
2830bf19 RR |
274 | } |
275 | } | |
6de97a3b | 276 | } |
c801d85f | 277 | |
03f38c58 | 278 | wxString wxTextCtrl::GetValue() const |
c801d85f | 279 | { |
05939a81 | 280 | wxCHECK_MSG( m_text != NULL, _T(""), _T("invalid text ctrl") ); |
8bbe427f | 281 | |
2830bf19 RR |
282 | wxString tmp; |
283 | if (m_windowStyle & wxTE_MULTILINE) | |
284 | { | |
285 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
286 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
dcf924a3 | 287 | tmp = wxString(text,*wxConvCurrent); |
2830bf19 RR |
288 | g_free( text ); |
289 | } | |
290 | else | |
291 | { | |
dcf924a3 | 292 | tmp = wxString(gtk_entry_get_text( GTK_ENTRY(m_text) ),*wxConvCurrent); |
2830bf19 RR |
293 | } |
294 | return tmp; | |
6de97a3b | 295 | } |
c801d85f KB |
296 | |
297 | void wxTextCtrl::SetValue( const wxString &value ) | |
298 | { | |
05939a81 | 299 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 300 | |
05939a81 | 301 | wxString tmp = _T(""); |
2830bf19 RR |
302 | if (!value.IsNull()) tmp = value; |
303 | if (m_windowStyle & wxTE_MULTILINE) | |
304 | { | |
305 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
306 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
307 | len = 0; | |
05939a81 | 308 | #if wxUSE_UNICODE |
3358d36e | 309 | wxWX2MBbuf tmpbuf = tmp.mbc_str(); |
05939a81 OK |
310 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmpbuf, strlen(tmpbuf), &len ); |
311 | #else | |
312 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp.mbc_str(), tmp.Length(), &len ); | |
313 | #endif | |
2830bf19 RR |
314 | } |
315 | else | |
316 | { | |
05939a81 | 317 | gtk_entry_set_text( GTK_ENTRY(m_text), tmp.mbc_str() ); |
2830bf19 | 318 | } |
6de97a3b | 319 | } |
c801d85f KB |
320 | |
321 | void wxTextCtrl::WriteText( const wxString &text ) | |
322 | { | |
05939a81 | 323 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 324 | |
2df7be7f | 325 | if (text.IsEmpty()) return; |
484e45bf | 326 | |
2830bf19 RR |
327 | if (m_windowStyle & wxTE_MULTILINE) |
328 | { | |
291a8f20 | 329 | /* this moves the cursor pos to behind the inserted text */ |
3358d36e VZ |
330 | gint len = GTK_EDITABLE(m_text)->current_pos; |
331 | ||
05939a81 | 332 | #if wxUSE_UNICODE |
3358d36e | 333 | wxWX2MBbuf buf = text.mbc_str(); |
05939a81 OK |
334 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len ); |
335 | #else | |
2830bf19 | 336 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); |
05939a81 | 337 | #endif |
3358d36e VZ |
338 | |
339 | /* bring editable's cursor uptodate. bug in GTK. */ | |
340 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
2830bf19 RR |
341 | } |
342 | else | |
343 | { | |
c46554be | 344 | /* this moves the cursor pos to behind the inserted text */ |
3358d36e | 345 | gint len = GTK_EDITABLE(m_text)->current_pos; |
05939a81 | 346 | #if wxUSE_UNICODE |
3358d36e | 347 | wxWX2MBbuf buf = text.mbc_str(); |
05939a81 OK |
348 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len ); |
349 | #else | |
c46554be | 350 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); |
05939a81 | 351 | #endif |
3358d36e VZ |
352 | |
353 | /* bring editable's cursor uptodate. bug in GTK. */ | |
354 | GTK_EDITABLE(m_text)->current_pos += text.Len(); | |
355 | ||
356 | /* bring entry's cursor uptodate. bug in GTK. */ | |
357 | gtk_entry_set_position( GTK_ENTRY(m_text), GTK_EDITABLE(m_text)->current_pos ); | |
2830bf19 | 358 | } |
6de97a3b | 359 | } |
c801d85f | 360 | |
a6e21573 HH |
361 | void wxTextCtrl::AppendText( const wxString &text ) |
362 | { | |
05939a81 | 363 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
a6e21573 | 364 | |
2df7be7f RR |
365 | if (text.IsEmpty()) return; |
366 | ||
a6e21573 HH |
367 | if (m_windowStyle & wxTE_MULTILINE) |
368 | { | |
bb69661b VZ |
369 | bool hasSpecialAttributes = m_font.Ok() || |
370 | m_foregroundColour.Ok() || | |
371 | m_backgroundColour.Ok(); | |
372 | if ( hasSpecialAttributes ) | |
373 | { | |
374 | gtk_text_insert( GTK_TEXT(m_text), | |
375 | m_font.GetInternalFont(), | |
376 | m_foregroundColour.GetColor(), | |
377 | m_backgroundColour.GetColor(), | |
c980c992 | 378 | text.mbc_str(), text.length()); |
bb69661b VZ |
379 | |
380 | } | |
381 | else | |
382 | { | |
383 | /* we'll insert at the last position */ | |
384 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
05939a81 | 385 | #if wxUSE_UNICODE |
bb69661b VZ |
386 | wxWX2MBbuf buf = text.mbc_str(); |
387 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &len ); | |
05939a81 | 388 | #else |
bb69661b | 389 | gtk_editable_insert_text( GTK_EDITABLE(m_text), text, text.Length(), &len ); |
05939a81 | 390 | #endif |
bb69661b | 391 | } |
3358d36e VZ |
392 | |
393 | /* bring editable's cursor uptodate. bug in GTK. */ | |
394 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
a6e21573 HH |
395 | } |
396 | else | |
397 | { | |
05939a81 | 398 | gtk_entry_append_text( GTK_ENTRY(m_text), text.mbc_str() ); |
a6e21573 HH |
399 | } |
400 | } | |
401 | ||
debe6624 | 402 | wxString wxTextCtrl::GetLineText( long lineNo ) const |
c801d85f | 403 | { |
a81258be RR |
404 | if (m_windowStyle & wxTE_MULTILINE) |
405 | { | |
406 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
407 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
408 | ||
409 | if (text) | |
410 | { | |
05939a81 | 411 | wxString buf(_T("")); |
a81258be RR |
412 | long i; |
413 | int currentLine = 0; | |
414 | for (i = 0; currentLine != lineNo && text[i]; i++ ) | |
415 | if (text[i] == '\n') | |
416 | currentLine++; | |
417 | // Now get the text | |
418 | int j; | |
419 | for (j = 0; text[i] && text[i] != '\n'; i++, j++ ) | |
420 | buf += text[i]; | |
8bbe427f | 421 | |
a81258be RR |
422 | g_free( text ); |
423 | return buf; | |
424 | } | |
425 | else | |
426 | return wxEmptyString; | |
427 | } | |
428 | else | |
429 | { | |
430 | if (lineNo == 0) return GetValue(); | |
431 | return wxEmptyString; | |
432 | } | |
6de97a3b | 433 | } |
c801d85f | 434 | |
a81258be RR |
435 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &WXUNUSED(event) ) |
436 | { | |
ac0d36b5 HH |
437 | /* If you implement this, don't forget to update the documentation! |
438 | * (file docs/latex/wx/text.tex) */ | |
05939a81 | 439 | wxFAIL_MSG( _T("wxTextCtrl::OnDropFiles not implemented") ); |
a81258be | 440 | } |
112892b9 | 441 | |
0efe5ba7 | 442 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const |
c801d85f | 443 | { |
96385642 | 444 | if ( m_windowStyle & wxTE_MULTILINE ) |
805dd538 | 445 | { |
96385642 VZ |
446 | wxString text = GetValue(); |
447 | ||
6085b116 | 448 | // cast to prevent warning. But pos really should've been unsigned. |
2829d9e3 | 449 | if( (unsigned long)pos > text.Len() ) |
96385642 VZ |
450 | return FALSE; |
451 | ||
ac0d36b5 HH |
452 | *x=0; // First Col |
453 | *y=0; // First Line | |
2829d9e3 | 454 | |
05939a81 OK |
455 | const wxChar* stop = text.c_str() + pos; |
456 | for ( const wxChar *p = text.c_str(); p < stop; p++ ) | |
96385642 | 457 | { |
05939a81 | 458 | if (*p == _T('\n')) |
96385642 VZ |
459 | { |
460 | (*y)++; | |
ac0d36b5 | 461 | *x=0; |
96385642 VZ |
462 | } |
463 | else | |
464 | (*x)++; | |
465 | } | |
805dd538 | 466 | } |
96385642 VZ |
467 | else // single line control |
468 | { | |
2829d9e3 | 469 | if ( pos <= GTK_ENTRY(m_text)->text_length ) |
96385642 | 470 | { |
ac0d36b5 | 471 | *y = 0; |
96385642 VZ |
472 | *x = pos; |
473 | } | |
474 | else | |
475 | { | |
476 | // index out of bounds | |
477 | return FALSE; | |
478 | } | |
8bbe427f | 479 | } |
96385642 VZ |
480 | |
481 | return TRUE; | |
6de97a3b | 482 | } |
c801d85f | 483 | |
e3ca08dd | 484 | long wxTextCtrl::XYToPosition(long x, long y ) const |
c801d85f | 485 | { |
2830bf19 | 486 | if (!(m_windowStyle & wxTE_MULTILINE)) return 0; |
805dd538 | 487 | |
2830bf19 | 488 | long pos=0; |
ac0d36b5 | 489 | for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n' |
8bbe427f | 490 | |
3358d36e | 491 | pos += x; |
2830bf19 | 492 | return pos; |
6de97a3b | 493 | } |
c801d85f | 494 | |
a81258be | 495 | int wxTextCtrl::GetLineLength(long lineNo) const |
c801d85f | 496 | { |
a81258be RR |
497 | wxString str = GetLineText (lineNo); |
498 | return (int) str.Length(); | |
6de97a3b | 499 | } |
c801d85f | 500 | |
a81258be | 501 | int wxTextCtrl::GetNumberOfLines() const |
c801d85f | 502 | { |
2830bf19 | 503 | if (m_windowStyle & wxTE_MULTILINE) |
a81258be | 504 | { |
2830bf19 RR |
505 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); |
506 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
507 | ||
508 | if (text) | |
509 | { | |
510 | int currentLine = 0; | |
511 | for (int i = 0; i < len; i++ ) | |
96385642 | 512 | { |
2830bf19 | 513 | if (text[i] == '\n') |
96385642 VZ |
514 | currentLine++; |
515 | } | |
2830bf19 | 516 | g_free( text ); |
2829d9e3 VZ |
517 | |
518 | // currentLine is 0 based, add 1 to get number of lines | |
519 | return currentLine + 1; | |
2830bf19 RR |
520 | } |
521 | else | |
96385642 | 522 | { |
2830bf19 | 523 | return 0; |
96385642 | 524 | } |
a81258be RR |
525 | } |
526 | else | |
2830bf19 | 527 | { |
96385642 | 528 | return 1; |
2830bf19 | 529 | } |
6de97a3b | 530 | } |
c801d85f | 531 | |
debe6624 | 532 | void wxTextCtrl::SetInsertionPoint( long pos ) |
c801d85f | 533 | { |
05939a81 | 534 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
3358d36e VZ |
535 | |
536 | if (m_windowStyle & wxTE_MULTILINE) | |
291a8f20 RR |
537 | { |
538 | /* seems to be broken in GTK 1.0.X: | |
3358d36e VZ |
539 | gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */ |
540 | ||
291a8f20 RR |
541 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_text), |
542 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e | 543 | |
291a8f20 | 544 | /* we fake a set_point by inserting and deleting. as the user |
3358d36e VZ |
545 | isn't supposed to get to know about thos non-sense, we |
546 | disconnect so that no events are sent to the user program. */ | |
547 | ||
291a8f20 RR |
548 | gint tmp = (gint)pos; |
549 | gtk_editable_insert_text( GTK_EDITABLE(m_text), " ", 1, &tmp ); | |
3358d36e VZ |
550 | gtk_editable_delete_text( GTK_EDITABLE(m_text), tmp-1, tmp ); |
551 | ||
291a8f20 RR |
552 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
553 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
3358d36e VZ |
554 | |
555 | /* bring editable's cursor uptodate. another bug in GTK. */ | |
556 | ||
557 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
ac0d36b5 | 558 | } |
2830bf19 | 559 | else |
291a8f20 | 560 | { |
d59051dd | 561 | gtk_entry_set_position( GTK_ENTRY(m_text), (int)pos ); |
3358d36e VZ |
562 | |
563 | /* bring editable's cursor uptodate. bug in GTK. */ | |
564 | ||
565 | GTK_EDITABLE(m_text)->current_pos = pos; | |
291a8f20 | 566 | } |
6de97a3b | 567 | } |
c801d85f | 568 | |
03f38c58 | 569 | void wxTextCtrl::SetInsertionPointEnd() |
c801d85f | 570 | { |
05939a81 | 571 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 572 | |
d59051dd VZ |
573 | if (m_windowStyle & wxTE_MULTILINE) |
574 | SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text))); | |
575 | else | |
576 | gtk_entry_set_position( GTK_ENTRY(m_text), -1 ); | |
6de97a3b | 577 | } |
c801d85f | 578 | |
debe6624 | 579 | void wxTextCtrl::SetEditable( bool editable ) |
c801d85f | 580 | { |
05939a81 | 581 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 582 | |
2830bf19 RR |
583 | if (m_windowStyle & wxTE_MULTILINE) |
584 | gtk_text_set_editable( GTK_TEXT(m_text), editable ); | |
585 | else | |
586 | gtk_entry_set_editable( GTK_ENTRY(m_text), editable ); | |
6de97a3b | 587 | } |
c801d85f | 588 | |
0efe5ba7 VZ |
589 | void wxTextCtrl::DiscardEdits() |
590 | { | |
591 | m_modified = FALSE; | |
592 | } | |
593 | ||
debe6624 | 594 | void wxTextCtrl::SetSelection( long from, long to ) |
c801d85f | 595 | { |
05939a81 | 596 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 597 | |
2830bf19 | 598 | gtk_editable_select_region( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 599 | } |
c801d85f | 600 | |
910f1f8c | 601 | void wxTextCtrl::ShowPosition( long WXUNUSED(pos) ) |
c801d85f | 602 | { |
910f1f8c | 603 | // SetInsertionPoint( pos ); |
6de97a3b | 604 | } |
c801d85f | 605 | |
03f38c58 | 606 | long wxTextCtrl::GetInsertionPoint() const |
c801d85f | 607 | { |
05939a81 | 608 | wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") ); |
8bbe427f | 609 | |
2830bf19 | 610 | return (long) GTK_EDITABLE(m_text)->current_pos; |
6de97a3b | 611 | } |
c801d85f | 612 | |
03f38c58 | 613 | long wxTextCtrl::GetLastPosition() const |
c801d85f | 614 | { |
05939a81 | 615 | wxCHECK_MSG( m_text != NULL, 0, _T("invalid text ctrl") ); |
8bbe427f | 616 | |
2830bf19 RR |
617 | int pos = 0; |
618 | if (m_windowStyle & wxTE_MULTILINE) | |
619 | pos = gtk_text_get_length( GTK_TEXT(m_text) ); | |
620 | else | |
621 | pos = GTK_ENTRY(m_text)->text_length; | |
805dd538 | 622 | |
ac0d36b5 | 623 | return (long)pos; |
6de97a3b | 624 | } |
c801d85f | 625 | |
debe6624 | 626 | void wxTextCtrl::Remove( long from, long to ) |
c801d85f | 627 | { |
05939a81 | 628 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 629 | |
2830bf19 | 630 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
6de97a3b | 631 | } |
c801d85f | 632 | |
debe6624 | 633 | void wxTextCtrl::Replace( long from, long to, const wxString &value ) |
c801d85f | 634 | { |
05939a81 | 635 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 636 | |
2830bf19 | 637 | gtk_editable_delete_text( GTK_EDITABLE(m_text), (gint)from, (gint)to ); |
bb69661b | 638 | |
2df7be7f RR |
639 | if (!value.IsEmpty()) |
640 | { | |
641 | gint pos = (gint)from; | |
05939a81 | 642 | #if wxUSE_UNICODE |
2df7be7f RR |
643 | wxWX2MBbuf buf = value.mbc_str(); |
644 | gtk_editable_insert_text( GTK_EDITABLE(m_text), buf, strlen(buf), &pos ); | |
05939a81 | 645 | #else |
2df7be7f | 646 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &pos ); |
05939a81 | 647 | #endif |
2df7be7f | 648 | } |
6de97a3b | 649 | } |
c801d85f | 650 | |
03f38c58 | 651 | void wxTextCtrl::Cut() |
c801d85f | 652 | { |
05939a81 | 653 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 654 | |
d345e841 | 655 | #if (GTK_MINOR_VERSION > 0) |
2830bf19 | 656 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text) ); |
75ed1d15 | 657 | #else |
2830bf19 | 658 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 659 | #endif |
6de97a3b | 660 | } |
c801d85f | 661 | |
03f38c58 | 662 | void wxTextCtrl::Copy() |
c801d85f | 663 | { |
05939a81 | 664 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 665 | |
d345e841 | 666 | #if (GTK_MINOR_VERSION > 0) |
2830bf19 | 667 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text) ); |
75ed1d15 | 668 | #else |
2830bf19 | 669 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 670 | #endif |
6de97a3b | 671 | } |
c801d85f | 672 | |
03f38c58 | 673 | void wxTextCtrl::Paste() |
c801d85f | 674 | { |
05939a81 | 675 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
8bbe427f | 676 | |
d345e841 | 677 | #if (GTK_MINOR_VERSION > 0) |
2830bf19 | 678 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text) ); |
75ed1d15 | 679 | #else |
2830bf19 | 680 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_text), 0 ); |
75ed1d15 | 681 | #endif |
6de97a3b | 682 | } |
c801d85f | 683 | |
ca8b28f2 JS |
684 | bool wxTextCtrl::CanCopy() const |
685 | { | |
686 | // Can copy if there's a selection | |
687 | long from, to; | |
688 | GetSelection(& from, & to); | |
689 | return (from != to) ; | |
690 | } | |
691 | ||
692 | bool wxTextCtrl::CanCut() const | |
693 | { | |
694 | // Can cut if there's a selection | |
695 | long from, to; | |
696 | GetSelection(& from, & to); | |
697 | return (from != to) ; | |
698 | } | |
699 | ||
700 | bool wxTextCtrl::CanPaste() const | |
701 | { | |
702 | return IsEditable() ; | |
703 | } | |
704 | ||
705 | // Undo/redo | |
706 | void wxTextCtrl::Undo() | |
707 | { | |
708 | // TODO | |
05939a81 | 709 | wxFAIL_MSG( _T("wxTextCtrl::Undo not implemented") ); |
ca8b28f2 JS |
710 | } |
711 | ||
712 | void wxTextCtrl::Redo() | |
713 | { | |
714 | // TODO | |
05939a81 | 715 | wxFAIL_MSG( _T("wxTextCtrl::Redo not implemented") ); |
ca8b28f2 JS |
716 | } |
717 | ||
718 | bool wxTextCtrl::CanUndo() const | |
719 | { | |
720 | // TODO | |
05939a81 | 721 | wxFAIL_MSG( _T("wxTextCtrl::CanUndo not implemented") ); |
ca8b28f2 JS |
722 | return FALSE; |
723 | } | |
724 | ||
725 | bool wxTextCtrl::CanRedo() const | |
726 | { | |
727 | // TODO | |
05939a81 | 728 | wxFAIL_MSG( _T("wxTextCtrl::CanRedo not implemented") ); |
ca8b28f2 JS |
729 | return FALSE; |
730 | } | |
731 | ||
732 | // If the return values from and to are the same, there is no | |
733 | // selection. | |
734 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
735 | { | |
05060eeb | 736 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
bb69661b | 737 | |
05060eeb RR |
738 | if (!(GTK_EDITABLE(m_text)->has_selection)) |
739 | { | |
740 | if (from) *from = 0; | |
bb69661b VZ |
741 | if (to) *to = 0; |
742 | return; | |
05060eeb | 743 | } |
bb69661b | 744 | |
05060eeb RR |
745 | if (from) *from = (long) GTK_EDITABLE(m_text)->selection_start_pos; |
746 | if (to) *to = (long) GTK_EDITABLE(m_text)->selection_end_pos; | |
ca8b28f2 JS |
747 | } |
748 | ||
749 | bool wxTextCtrl::IsEditable() const | |
750 | { | |
05060eeb RR |
751 | wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") ); |
752 | ||
753 | return GTK_EDITABLE(m_text)->editable; | |
ca8b28f2 JS |
754 | } |
755 | ||
0efe5ba7 VZ |
756 | bool wxTextCtrl::IsModified() const |
757 | { | |
758 | return m_modified; | |
759 | } | |
760 | ||
03f38c58 | 761 | void wxTextCtrl::Clear() |
c801d85f | 762 | { |
05939a81 | 763 | SetValue( _T("") ); |
6de97a3b | 764 | } |
c801d85f | 765 | |
903f689b | 766 | void wxTextCtrl::OnChar( wxKeyEvent &key_event ) |
c801d85f | 767 | { |
05939a81 | 768 | wxCHECK_RET( m_text != NULL, _T("invalid text ctrl") ); |
805dd538 | 769 | |
2830bf19 RR |
770 | if ((key_event.KeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
771 | { | |
772 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
773 | event.SetEventObject(this); | |
774 | if (GetEventHandler()->ProcessEvent(event)) return; | |
775 | } | |
903f689b | 776 | |
2830bf19 | 777 | key_event.Skip(); |
6de97a3b | 778 | } |
c801d85f | 779 | |
03f38c58 | 780 | GtkWidget* wxTextCtrl::GetConnectWidget() |
e3e65dac | 781 | { |
ae0bdb01 | 782 | return GTK_WIDGET(m_text); |
6de97a3b | 783 | } |
e3e65dac | 784 | |
903f689b RR |
785 | bool wxTextCtrl::IsOwnGtkWindow( GdkWindow *window ) |
786 | { | |
ae0bdb01 RR |
787 | if (m_windowStyle & wxTE_MULTILINE) |
788 | return (window == GTK_TEXT(m_text)->text_area); | |
789 | else | |
790 | return (window == GTK_ENTRY(m_text)->text_area); | |
903f689b | 791 | } |
e3e65dac | 792 | |
bb69661b VZ |
793 | // the font will change for subsequent text insertiongs |
794 | bool wxTextCtrl::SetFont( const wxFont &font ) | |
868a2826 | 795 | { |
f03fc89f | 796 | wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") ); |
8bbe427f | 797 | |
bb69661b VZ |
798 | if ( !wxWindowBase::SetFont(font) ) |
799 | { | |
800 | // font didn't change, nothing to do | |
801 | return FALSE; | |
802 | } | |
803 | ||
804 | if ( m_windowStyle & wxTE_MULTILINE ) | |
805 | { | |
806 | // for compatibility with other ports: the font is a global controls | |
807 | // characteristic, so change the font globally | |
808 | wxString value = GetValue(); | |
809 | if ( !value.IsEmpty() ) | |
810 | { | |
811 | Clear(); | |
812 | ||
813 | AppendText(value); | |
814 | } | |
815 | } | |
816 | ||
817 | return TRUE; | |
58614078 RR |
818 | } |
819 | ||
f03fc89f | 820 | bool wxTextCtrl::SetForegroundColour( const wxColour &WXUNUSED(colour) ) |
58614078 | 821 | { |
f03fc89f | 822 | wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") ); |
8bbe427f | 823 | |
ae0bdb01 | 824 | // doesn't work |
f03fc89f | 825 | return FALSE; |
868a2826 | 826 | } |
e3e65dac | 827 | |
f03fc89f | 828 | bool wxTextCtrl::SetBackgroundColour( const wxColour &colour ) |
68dda785 | 829 | { |
f03fc89f | 830 | wxCHECK_MSG( m_text != NULL, FALSE, _T("invalid text ctrl") ); |
a81258be | 831 | |
ae0bdb01 | 832 | wxControl::SetBackgroundColour( colour ); |
3358d36e | 833 | |
f03fc89f VZ |
834 | if (!m_widget->window) |
835 | return FALSE; | |
8bbe427f | 836 | |
ae0bdb01 | 837 | wxColour sysbg = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ); |
805dd538 VZ |
838 | if (sysbg.Red() == colour.Red() && |
839 | sysbg.Green() == colour.Green() && | |
ae0bdb01 RR |
840 | sysbg.Blue() == colour.Blue()) |
841 | { | |
f03fc89f | 842 | return FALSE; // FIXME or TRUE? |
805dd538 VZ |
843 | } |
844 | ||
f03fc89f VZ |
845 | if (!m_backgroundColour.Ok()) |
846 | return FALSE; | |
8bbe427f | 847 | |
ae0bdb01 RR |
848 | if (m_windowStyle & wxTE_MULTILINE) |
849 | { | |
850 | GdkWindow *window = GTK_TEXT(m_text)->text_area; | |
f03fc89f VZ |
851 | if (!window) |
852 | return FALSE; | |
ae0bdb01 RR |
853 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); |
854 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
855 | gdk_window_clear( window ); | |
856 | } | |
f03fc89f VZ |
857 | |
858 | return TRUE; | |
58614078 RR |
859 | } |
860 | ||
861 | void wxTextCtrl::ApplyWidgetStyle() | |
862 | { | |
ae0bdb01 RR |
863 | if (m_windowStyle & wxTE_MULTILINE) |
864 | { | |
2830bf19 | 865 | // how ? |
805dd538 | 866 | } |
ae0bdb01 RR |
867 | else |
868 | { | |
869 | SetWidgetStyle(); | |
870 | gtk_widget_set_style( m_text, m_widgetStyle ); | |
871 | } | |
68dda785 | 872 | } |
f96aa4d9 | 873 | |
e702ff0f JS |
874 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
875 | { | |
876 | Cut(); | |
877 | } | |
878 | ||
879 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
880 | { | |
881 | Copy(); | |
882 | } | |
883 | ||
884 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
885 | { | |
886 | Paste(); | |
887 | } | |
888 | ||
889 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
890 | { | |
891 | Undo(); | |
892 | } | |
893 | ||
894 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
895 | { | |
896 | Redo(); | |
897 | } | |
898 | ||
899 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
900 | { | |
901 | event.Enable( CanCut() ); | |
902 | } | |
903 | ||
904 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
905 | { | |
906 | event.Enable( CanCopy() ); | |
907 | } | |
908 | ||
909 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
910 | { | |
911 | event.Enable( CanPaste() ); | |
912 | } | |
913 | ||
914 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
915 | { | |
916 | event.Enable( CanUndo() ); | |
917 | } | |
918 | ||
919 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
920 | { | |
921 | event.Enable( CanRedo() ); | |
922 | } |