]>
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 ); |
0c77152e | 45 | event.SetString( copystring(win->GetValue()) ); |
2830bf19 RR |
46 | event.SetEventObject( win ); |
47 | win->GetEventHandler()->ProcessEvent( event ); | |
0c77152e | 48 | delete[] event.GetString(); |
6de97a3b | 49 | } |
112892b9 | 50 | |
2830bf19 RR |
51 | //----------------------------------------------------------------------------- |
52 | // "size_allocate" | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
805dd538 | 55 | static void |
2830bf19 RR |
56 | gtk_text_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* WXUNUSED(alloc), wxTextCtrl *win ) |
57 | { | |
58 | win->CalculateScrollbar(); | |
59 | } | |
60 | ||
2f2aa628 RR |
61 | //----------------------------------------------------------------------------- |
62 | // wxTextCtrl | |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
66 | ||
c801d85f | 67 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
2830bf19 | 68 | EVT_CHAR(wxTextCtrl::OnChar) |
c801d85f KB |
69 | END_EVENT_TABLE() |
70 | ||
f5abe911 | 71 | #ifndef NO_TEXT_WINDOW_STREAM |
03f38c58 | 72 | wxTextCtrl::wxTextCtrl() : streambuf() |
c801d85f | 73 | { |
2830bf19 | 74 | if (allocate()) setp(base(),ebuf()); |
13289f04 | 75 | |
2830bf19 | 76 | m_modified = FALSE; |
6de97a3b | 77 | } |
f5abe911 RR |
78 | #else |
79 | wxTextCtrl::wxTextCtrl() | |
80 | { | |
81 | m_modified = FALSE; | |
82 | } | |
83 | #endif | |
c801d85f | 84 | |
f5abe911 | 85 | #ifndef NO_TEXT_WINDOW_STREAM |
debe6624 | 86 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 87 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 88 | int style, const wxValidator& validator, const wxString &name ) : streambuf() |
c801d85f | 89 | { |
2830bf19 | 90 | if (allocate()) setp(base(),ebuf()); |
13289f04 | 91 | |
2830bf19 RR |
92 | m_modified = FALSE; |
93 | Create( parent, id, value, pos, size, style, validator, name ); | |
6de97a3b | 94 | } |
f5abe911 RR |
95 | #else |
96 | wxTextCtrl::wxTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value, | |
97 | const wxPoint &pos, const wxSize &size, | |
98 | int style, const wxValidator& validator, const wxString &name ) | |
99 | { | |
100 | m_modified = FALSE; | |
101 | Create( parent, id, value, pos, size, style, validator, name ); | |
102 | } | |
103 | #endif | |
c801d85f | 104 | |
debe6624 | 105 | bool wxTextCtrl::Create( wxWindow *parent, wxWindowID id, const wxString &value, |
484e45bf | 106 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 107 | int style, const wxValidator& validator, const wxString &name ) |
c801d85f | 108 | { |
2830bf19 | 109 | m_needParent = TRUE; |
b292e2f5 | 110 | m_acceptsFocus = TRUE; |
484e45bf | 111 | |
2830bf19 | 112 | PreCreation( parent, id, pos, size, style, name ); |
6de97a3b | 113 | |
2830bf19 | 114 | SetValidator( validator ); |
805dd538 | 115 | |
2830bf19 | 116 | m_vScrollbarVisible = TRUE; |
13289f04 | 117 | |
2830bf19 RR |
118 | bool multi_line = (style & wxTE_MULTILINE) != 0; |
119 | if ( multi_line ) | |
120 | { | |
121 | // a multi-line edit control: create a vertical scrollbar by default and | |
122 | // horizontal if requested | |
123 | bool bHasHScrollbar = (style & wxHSCROLL) != 0; | |
805dd538 | 124 | |
2830bf19 RR |
125 | // create our control... |
126 | m_text = gtk_text_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); | |
127 | ||
128 | // ... and put into the upper left hand corner of the table | |
129 | m_widget = gtk_table_new(bHasHScrollbar ? 2 : 1, 2, FALSE); | |
5664fc32 RR |
130 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); |
131 | ||
2830bf19 | 132 | gtk_table_attach( GTK_TABLE(m_widget), m_text, 0, 1, 0, 1, |
41dee9d0 | 133 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
f5368809 RR |
134 | (GtkAttachOptions)(GTK_FILL | GTK_EXPAND | GTK_SHRINK), |
135 | 0, 0); | |
13289f04 | 136 | |
2830bf19 RR |
137 | // put the horizontal scrollbar in the lower left hand corner |
138 | if (bHasHScrollbar) | |
139 | { | |
140 | GtkWidget *hscrollbar = gtk_hscrollbar_new(GTK_TEXT(m_text)->hadj); | |
5664fc32 RR |
141 | GTK_WIDGET_UNSET_FLAGS( hscrollbar, GTK_CAN_FOCUS ); |
142 | ||
2830bf19 | 143 | gtk_table_attach(GTK_TABLE(m_widget), hscrollbar, 0, 1, 1, 2, |
f5368809 | 144 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), |
13289f04 VZ |
145 | GTK_FILL, |
146 | 0, 0); | |
2830bf19 RR |
147 | gtk_widget_show(hscrollbar); |
148 | } | |
13289f04 | 149 | |
2830bf19 RR |
150 | // finally, put the vertical scrollbar in the upper right corner |
151 | m_vScrollbar = gtk_vscrollbar_new( GTK_TEXT(m_text)->vadj ); | |
5664fc32 RR |
152 | GTK_WIDGET_UNSET_FLAGS( m_vScrollbar, GTK_CAN_FOCUS ); |
153 | ||
2830bf19 | 154 | gtk_table_attach(GTK_TABLE(m_widget), m_vScrollbar, 1, 2, 0, 1, |
13289f04 | 155 | GTK_FILL, |
f5368809 | 156 | (GtkAttachOptions)(GTK_EXPAND | GTK_FILL | GTK_SHRINK), |
13289f04 | 157 | 0, 0); |
2830bf19 | 158 | gtk_widget_show( m_vScrollbar ); |
805dd538 | 159 | |
2830bf19 RR |
160 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
161 | GTK_SIGNAL_FUNC(gtk_text_size_callback), (gpointer)this ); | |
162 | } | |
163 | else | |
164 | { | |
165 | // a single-line text control: no need for scrollbars | |
166 | m_widget = | |
167 | m_text = gtk_entry_new(); | |
168 | } | |
484e45bf | 169 | |
2830bf19 RR |
170 | wxSize newSize = size; |
171 | if (newSize.x == -1) newSize.x = 80; | |
172 | if (newSize.y == -1) newSize.y = 26; | |
173 | SetSize( newSize.x, newSize.y ); | |
484e45bf | 174 | |
2830bf19 | 175 | m_parent->AddChild( this ); |
6ca41e57 | 176 | |
2830bf19 | 177 | (m_parent->m_insertCallback)( m_parent, this ); |
8bbe427f | 178 | |
2830bf19 | 179 | PostCreation(); |
484e45bf | 180 | |
2830bf19 RR |
181 | if (multi_line) |
182 | { | |
183 | gtk_widget_realize(m_text); | |
184 | gtk_widget_show(m_text); | |
185 | } | |
13289f04 | 186 | |
2830bf19 | 187 | // we want to be notified about text changes |
b292e2f5 RR |
188 | gtk_signal_connect( GTK_OBJECT(m_text), "changed", |
189 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); | |
484e45bf | 190 | |
291a8f20 | 191 | if (!value.IsEmpty()) |
2830bf19 RR |
192 | { |
193 | gint tmp = 0; | |
194 | gtk_editable_insert_text( GTK_EDITABLE(m_text), value, value.Length(), &tmp ); | |
291a8f20 RR |
195 | |
196 | if (multi_line) | |
197 | { | |
198 | /* bring editable's cursor uptodate. bug in GTK. */ | |
199 | ||
200 | GTK_EDITABLE(m_text)->current_pos = gtk_text_get_point( GTK_TEXT(m_text) ); | |
201 | } | |
2830bf19 | 202 | } |
484e45bf | 203 | |
2830bf19 RR |
204 | if (style & wxTE_PASSWORD) |
205 | { | |
206 | if (!multi_line) | |
207 | gtk_entry_set_visibility( GTK_ENTRY(m_text), FALSE ); | |
208 | } | |
8bbe427f | 209 | |
2830bf19 RR |
210 | if (style & wxTE_READONLY) |
211 | { | |
212 | if (!multi_line) | |
213 | gtk_entry_set_editable( GTK_ENTRY(m_text), FALSE ); | |
214 | } | |
215 | else | |
216 | { | |
217 | if (multi_line) | |
218 | gtk_text_set_editable( GTK_TEXT(m_text), 1 ); | |
219 | } | |
805dd538 | 220 | |
2830bf19 | 221 | Show( TRUE ); |
805dd538 | 222 | |
2830bf19 RR |
223 | SetBackgroundColour( parent->GetBackgroundColour() ); |
224 | SetForegroundColour( parent->GetForegroundColour() ); | |
484e45bf | 225 | |
2830bf19 RR |
226 | return TRUE; |
227 | } | |
484e45bf | 228 | |
2830bf19 RR |
229 | void wxTextCtrl::CalculateScrollbar() |
230 | { | |
231 | if ((m_windowStyle & wxTE_MULTILINE) == 0) return; | |
f96aa4d9 | 232 | |
2830bf19 | 233 | GtkAdjustment *adj = GTK_TEXT(m_text)->vadj; |
805dd538 | 234 | |
2830bf19 RR |
235 | if (adj->upper - adj->page_size < 0.8) |
236 | { | |
237 | if (m_vScrollbarVisible) | |
238 | { | |
805dd538 VZ |
239 | gtk_widget_hide( m_vScrollbar ); |
240 | ||
241 | m_vScrollbarVisible = FALSE; | |
2830bf19 RR |
242 | } |
243 | } | |
244 | else | |
245 | { | |
246 | if (!m_vScrollbarVisible) | |
247 | { | |
805dd538 VZ |
248 | gtk_widget_show( m_vScrollbar ); |
249 | ||
250 | m_vScrollbarVisible = TRUE; | |
2830bf19 RR |
251 | } |
252 | } | |
6de97a3b | 253 | } |
c801d85f | 254 | |
03f38c58 | 255 | wxString wxTextCtrl::GetValue() const |
c801d85f | 256 | { |
2830bf19 | 257 | wxCHECK_MSG( m_text != NULL, "", "invalid text ctrl" ); |
8bbe427f | 258 | |
2830bf19 RR |
259 | wxString tmp; |
260 | if (m_windowStyle & wxTE_MULTILINE) | |
261 | { | |
262 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
263 | char *text = gtk_editable_get_chars( GTK_EDITABLE(m_text), 0, len ); | |
264 | tmp = text; | |
265 | g_free( text ); | |
266 | } | |
267 | else | |
268 | { | |
269 | tmp = gtk_entry_get_text( GTK_ENTRY(m_text) ); | |
270 | } | |
271 | return tmp; | |
6de97a3b | 272 | } |
c801d85f KB |
273 | |
274 | void wxTextCtrl::SetValue( const wxString &value ) | |
275 | { | |
2830bf19 | 276 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 277 | |
2830bf19 RR |
278 | wxString tmp = ""; |
279 | if (!value.IsNull()) tmp = value; | |
280 | if (m_windowStyle & wxTE_MULTILINE) | |
281 | { | |
282 | gint len = gtk_text_get_length( GTK_TEXT(m_text) ); | |
283 | gtk_editable_delete_text( GTK_EDITABLE(m_text), 0, len ); | |
284 | len = 0; | |
285 | gtk_editable_insert_text( GTK_EDITABLE(m_text), tmp, tmp.Length(), &len ); | |
286 | } | |
287 | else | |
288 | { | |
289 | gtk_entry_set_text( GTK_ENTRY(m_text), tmp ); | |
290 | } | |
6de97a3b | 291 | } |
c801d85f KB |
292 | |
293 | void wxTextCtrl::WriteText( const wxString &text ) | |
294 | { | |
2830bf19 | 295 | wxCHECK_RET( m_text != NULL, "invalid text ctrl" ); |
8bbe427f | 296 | |
2830bf19 | 297 | if (text.IsNull()) return; |
484e45bf | 298 | |
2830bf19 RR |
299 | if (m_windowStyle & wxTE_MULTILINE) |
300 | { | |
291a8f20 | 301 | /* this moves the cursor pos to behind the inserted text */ |
ac0d36b5 | 302 | gint len = GTK_EDITABLE(m_text)->current_pos; |
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 |