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