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