]> git.saurik.com Git - wxWidgets.git/blame - src/motif/textctrl.cpp
fixed compilation under Motif
[wxWidgets.git] / src / motif / textctrl.cpp
CommitLineData
4bb6408c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose: wxTextCtrl
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
66a007fb 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
66a007fb
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
4bb6408c 20#ifdef __GNUG__
66a007fb 21 #pragma implementation "textctrl.h"
4bb6408c
JS
22#endif
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fstream.h>
66a007fb 27#include <ctype.h>
4bb6408c
JS
28
29#include "wx/textctrl.h"
30#include "wx/settings.h"
31#include "wx/filefn.h"
32#include "wx/utils.h"
33
02e8b2f9 34#include <Xm/Text.h>
02e8b2f9
JS
35
36#include "wx/motif/private.h"
37
66a007fb
VZ
38// ----------------------------------------------------------------------------
39// private functions
40// ----------------------------------------------------------------------------
41
c27eab7e
VZ
42// helper: inserts the new text in the value of the text ctrl and returns the
43// result in place
44static void MergeChangesIntoString(wxString& value,
45 XmTextVerifyCallbackStruct *textStruct);
46
66a007fb
VZ
47// callbacks
48static void wxTextWindowChangedProc(Widget w, XtPointer clientData, XtPointer ptr);
49static void wxTextWindowModifyProc(Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs);
50static void wxTextWindowGainFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs);
51static void wxTextWindowLoseFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs);
52static void wxTextWindowActivateProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *ptr);
02e8b2f9 53
4bb6408c 54#if !USE_SHARED_LIBRARY
66a007fb 55 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
4bb6408c 56
66a007fb
VZ
57 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
58 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
59 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
60
61 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
62 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
63 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
64 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
65 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
66
67 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
68 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
69 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
70 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
71 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
72
66a007fb 73 END_EVENT_TABLE()
4bb6408c
JS
74#endif
75
66a007fb
VZ
76// ============================================================================
77// implementation
78// ============================================================================
79
80// ----------------------------------------------------------------------------
81// wxTextCtrl
82// ----------------------------------------------------------------------------
83
4bb6408c
JS
84// Text item
85wxTextCtrl::wxTextCtrl()
86#ifndef NO_TEXT_WINDOW_STREAM
66a007fb 87 : streambuf()
4bb6408c
JS
88#endif
89{
02e8b2f9
JS
90 m_tempCallbackStruct = (void*) NULL;
91 m_modified = FALSE;
dfc54541 92 m_processedDefault = FALSE;
4bb6408c
JS
93}
94
66a007fb
VZ
95bool wxTextCtrl::Create(wxWindow *parent,
96 wxWindowID id,
2d120f83
JS
97 const wxString& value,
98 const wxPoint& pos,
66a007fb
VZ
99 const wxSize& size,
100 long style,
2d120f83
JS
101 const wxValidator& validator,
102 const wxString& name)
4bb6408c 103{
02e8b2f9
JS
104 m_tempCallbackStruct = (void*) NULL;
105 m_modified = FALSE;
dfc54541 106 m_processedDefault = FALSE;
94b49b93
JS
107 // m_backgroundColour = parent->GetBackgroundColour();
108 m_backgroundColour = * wxWHITE;
0d57be45 109 m_foregroundColour = parent->GetForegroundColour();
66a007fb 110
4bb6408c
JS
111 SetName(name);
112 SetValidator(validator);
66a007fb
VZ
113 if (parent)
114 parent->AddChild(this);
115
4bb6408c 116 m_windowStyle = style;
66a007fb 117
4bb6408c 118 if ( id == -1 )
2d120f83 119 m_windowId = (int)NewControlId();
4bb6408c 120 else
2d120f83 121 m_windowId = id;
66a007fb 122
02e8b2f9 123 Widget parentWidget = (Widget) parent->GetClientWidget();
66a007fb 124
02e8b2f9 125 bool wantHorizScrolling = ((m_windowStyle & wxHSCROLL) != 0);
66a007fb 126
02e8b2f9
JS
127 // If we don't have horizontal scrollbars, we want word wrap.
128 bool wantWordWrap = !wantHorizScrolling;
66a007fb 129
02e8b2f9
JS
130 if (m_windowStyle & wxTE_MULTILINE)
131 {
132 Arg args[2];
133 XtSetArg (args[0], XmNscrollHorizontal, wantHorizScrolling ? True : False);
134 XtSetArg (args[1], XmNwordWrap, wantWordWrap ? True : False);
66a007fb
VZ
135
136 m_mainWidget = (WXWidget) XmCreateScrolledText(parentWidget,
137 (char*)name.c_str(),
138 args, 2);
139
02e8b2f9 140 XtVaSetValues ((Widget) m_mainWidget,
66a007fb
VZ
141 XmNeditable, ((style & wxTE_READONLY) ? False : True),
142 XmNeditMode, XmMULTI_LINE_EDIT,
143 NULL);
02e8b2f9
JS
144 XtManageChild ((Widget) m_mainWidget);
145 }
146 else
147 {
66a007fb
VZ
148 m_mainWidget = (WXWidget)XtVaCreateManagedWidget
149 (
150 (char*)name.c_str(),
151 xmTextWidgetClass,
152 parentWidget,
153 NULL
154 );
155
02e8b2f9
JS
156 // TODO: Is this relevant? What does it do?
157 int noCols = 2;
158 if (!value.IsNull() && (value.Length() > (unsigned int) noCols))
159 noCols = value.Length();
66a007fb
VZ
160 XtVaSetValues((Widget) m_mainWidget,
161 XmNcolumns, noCols,
162 NULL);
02e8b2f9 163 }
66a007fb
VZ
164
165 // remove border if asked for
166 if ( style & wxNO_BORDER )
167 {
168 XtVaSetValues((Widget)m_mainWidget,
169 XmNshadowThickness, 0,
170 NULL);
171 }
172
173 if ( !!value )
174 XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str());
175
176 // install callbacks
02e8b2f9 177 XtAddCallback((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc)wxTextWindowChangedProc, (XtPointer)this);
66a007fb 178
02e8b2f9 179 XtAddCallback((Widget) m_mainWidget, XmNmodifyVerifyCallback, (XtCallbackProc)wxTextWindowModifyProc, (XtPointer)this);
66a007fb 180
dfc54541 181 XtAddCallback((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc)wxTextWindowActivateProc, (XtPointer)this);
66a007fb 182
02e8b2f9 183 XtAddCallback((Widget) m_mainWidget, XmNfocusCallback, (XtCallbackProc)wxTextWindowGainFocusProc, (XtPointer)this);
66a007fb 184
02e8b2f9 185 XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this);
66a007fb
VZ
186
187 // font
4b5f3fe6
JS
188 m_windowFont = parent->GetFont();
189 ChangeFont(FALSE);
66a007fb 190
02e8b2f9
JS
191 SetCanAddEventHandler(TRUE);
192 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
66a007fb 193
0d57be45 194 ChangeBackgroundColour();
66a007fb 195
4bb6408c
JS
196 return TRUE;
197}
198
02e8b2f9 199WXWidget wxTextCtrl::GetTopWidget() const
4bb6408c 200{
dfc54541 201 return ((m_windowStyle & wxTE_MULTILINE) ? (WXWidget) XtParent((Widget) m_mainWidget) : m_mainWidget);
4bb6408c
JS
202}
203
02e8b2f9 204wxString wxTextCtrl::GetValue() const
4bb6408c 205{
c27eab7e
VZ
206 wxString str; // result
207
dfc54541 208 if (m_windowStyle & wxTE_PASSWORD)
c27eab7e
VZ
209 {
210 // the value is stored always in m_value because it can't be retrieved
211 // from the text control
212 str = m_value;
213 }
dfc54541
JS
214 else
215 {
c27eab7e 216 // just get the string from Motif
dfc54541 217 char *s = XmTextGetString ((Widget) m_mainWidget);
c27eab7e 218 if ( s )
dfc54541 219 {
c27eab7e 220 str = s;
dfc54541 221 XtFree (s);
2d120f83 222 }
c27eab7e
VZ
223 //else: return empty string
224
225 if ( m_tempCallbackStruct )
dfc54541 226 {
c27eab7e
VZ
227 // the string in the control isn't yet updated, can't use it as is
228 MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *)
229 m_tempCallbackStruct);
dfc54541
JS
230 }
231 }
c27eab7e
VZ
232
233 return str;
4bb6408c
JS
234}
235
02e8b2f9 236void wxTextCtrl::SetValue(const wxString& value)
4bb6408c 237{
dfc54541 238 m_inSetValue = TRUE;
66a007fb
VZ
239
240 XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str());
241
dfc54541 242 m_inSetValue = FALSE;
4bb6408c
JS
243}
244
245// Clipboard operations
246void wxTextCtrl::Copy()
247{
dfc54541 248 XmTextCopy((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
249}
250
251void wxTextCtrl::Cut()
252{
dfc54541 253 XmTextCut((Widget) m_mainWidget, CurrentTime);
4bb6408c
JS
254}
255
256void wxTextCtrl::Paste()
257{
dfc54541 258 XmTextPaste((Widget) m_mainWidget);
4bb6408c
JS
259}
260
ca8b28f2
JS
261bool wxTextCtrl::CanCopy() const
262{
263 // Can copy if there's a selection
264 long from, to;
265 GetSelection(& from, & to);
266 return (from != to) ;
267}
268
269bool wxTextCtrl::CanCut() const
270{
271 // Can cut if there's a selection
272 long from, to;
273 GetSelection(& from, & to);
274 return (from != to) ;
275}
276
277bool wxTextCtrl::CanPaste() const
278{
279 return IsEditable() ;
280}
281
282// Undo/redo
283void wxTextCtrl::Undo()
284{
285 // Not possible in Motif
286}
287
288void wxTextCtrl::Redo()
289{
290 // Not possible in Motif
291}
292
293bool wxTextCtrl::CanUndo() const
294{
295 // No Undo in Motif
296 return FALSE;
297}
298
299bool wxTextCtrl::CanRedo() const
300{
301 // No Redo in Motif
302 return FALSE;
303}
304
305// If the return values from and to are the same, there is no
306// selection.
307void wxTextCtrl::GetSelection(long* from, long* to) const
308{
309 XmTextPosition left, right;
310
311 XmTextGetSelectionPosition((Widget) m_mainWidget, & left, & right);
312
313 *from = (long) left;
314 *to = (long) right;
315}
316
317bool wxTextCtrl::IsEditable() const
318{
319 return (XmTextGetEditable((Widget) m_mainWidget) != 0);
320}
321
4bb6408c
JS
322void wxTextCtrl::SetEditable(bool editable)
323{
dfc54541 324 XmTextSetEditable((Widget) m_mainWidget, (Boolean) editable);
4bb6408c
JS
325}
326
327void wxTextCtrl::SetInsertionPoint(long pos)
328{
dfc54541 329 XmTextSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
330}
331
332void wxTextCtrl::SetInsertionPointEnd()
333{
334 long pos = GetLastPosition();
335 SetInsertionPoint(pos);
336}
337
338long wxTextCtrl::GetInsertionPoint() const
339{
dfc54541 340 return (long) XmTextGetInsertionPosition ((Widget) m_mainWidget);
4bb6408c
JS
341}
342
343long wxTextCtrl::GetLastPosition() const
344{
dfc54541 345 return (long) XmTextGetLastPosition ((Widget) m_mainWidget);
4bb6408c
JS
346}
347
348void wxTextCtrl::Replace(long from, long to, const wxString& value)
349{
dfc54541 350 XmTextReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
2d120f83 351 (char*) (const char*) value);
4bb6408c
JS
352}
353
354void wxTextCtrl::Remove(long from, long to)
355{
dfc54541 356 XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
66a007fb 357 (Time) 0);
dfc54541 358 XmTextRemove ((Widget) m_mainWidget);
4bb6408c
JS
359}
360
361void wxTextCtrl::SetSelection(long from, long to)
362{
dfc54541 363 XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
66a007fb 364 (Time) 0);
4bb6408c
JS
365}
366
367bool wxTextCtrl::LoadFile(const wxString& file)
368{
369 if (!wxFileExists(file))
370 return FALSE;
66a007fb 371
4bb6408c 372 m_fileName = file;
66a007fb 373
4bb6408c 374 Clear();
66a007fb 375
1a3ac83f
JS
376 Widget textWidget = (Widget) m_mainWidget;
377 FILE *fp;
66a007fb 378
1a3ac83f
JS
379 struct stat statb;
380 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
2d120f83 381 !(fp = fopen ((char*) (const char*) file, "r")))
4bb6408c 382 {
2d120f83 383 return FALSE;
1a3ac83f
JS
384 }
385 else
386 {
2d120f83
JS
387 long len = statb.st_size;
388 char *text;
389 if (!(text = XtMalloc ((unsigned) (len + 1))))
390 {
391 fclose (fp);
392 return FALSE;
393 }
394 if (fread (text, sizeof (char), len, fp) != (size_t) len)
395 {
396 }
397 fclose (fp);
66a007fb 398
2d120f83
JS
399 text[len] = 0;
400 XmTextSetString (textWidget, text);
401 // m_textPosition = len;
402 XtFree (text);
403 m_modified = FALSE;
404 return TRUE;
4bb6408c 405 }
4bb6408c
JS
406}
407
408// If file is null, try saved file name first
409// Returns TRUE if succeeds.
410bool wxTextCtrl::SaveFile(const wxString& file)
411{
412 wxString theFile(file);
413 if (theFile == "")
414 theFile = m_fileName;
415 if (theFile == "")
416 return FALSE;
417 m_fileName = theFile;
66a007fb 418
2d120f83
JS
419 Widget textWidget = (Widget) m_mainWidget;
420 FILE *fp;
66a007fb 421
2d120f83 422 if (!(fp = fopen ((char*) (const char*) theFile, "w")))
1a3ac83f 423 {
2d120f83 424 return FALSE;
1a3ac83f 425 }
2d120f83 426 else
1a3ac83f 427 {
2d120f83
JS
428 char *text = XmTextGetString (textWidget);
429 long len = XmTextGetLastPosition (textWidget);
66a007fb 430
2d120f83
JS
431 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
432 {
433 // Did not write whole file
434 }
435 // Make sure newline terminates the file
436 if (text[len - 1] != '\n')
437 fputc ('\n', fp);
66a007fb 438
2d120f83
JS
439 fclose (fp);
440 XtFree (text);
441 m_modified = FALSE;
442 return TRUE;
1a3ac83f 443 }
4bb6408c
JS
444}
445
446void wxTextCtrl::WriteText(const wxString& text)
447{
dfc54541
JS
448 long textPosition = GetInsertionPoint() + strlen (text);
449 XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), (char*) (const char*) text);
450 XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL);
451 SetInsertionPoint(textPosition);
452 XmTextShowPosition ((Widget) m_mainWidget, textPosition);
453 m_modified = TRUE;
4bb6408c
JS
454}
455
4dba84be
JS
456void wxTextCtrl::AppendText(const wxString& text)
457{
458 long textPosition = GetLastPosition() + strlen(text);
459 XmTextInsert ((Widget) m_mainWidget, GetLastPosition(), (char*) (const char*) text);
460 XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL);
461 SetInsertionPoint(textPosition);
462 XmTextShowPosition ((Widget) m_mainWidget, textPosition);
463 m_modified = TRUE;
464}
465
4bb6408c
JS
466void wxTextCtrl::Clear()
467{
02e8b2f9 468 XmTextSetString ((Widget) m_mainWidget, "");
02e8b2f9 469 m_modified = FALSE;
4bb6408c
JS
470}
471
472bool wxTextCtrl::IsModified() const
473{
02e8b2f9 474 return m_modified;
4bb6408c
JS
475}
476
477// Makes 'unmodified'
478void wxTextCtrl::DiscardEdits()
479{
dfc54541
JS
480 XmTextSetString ((Widget) m_mainWidget, "");
481 m_modified = FALSE;
4bb6408c
JS
482}
483
484int wxTextCtrl::GetNumberOfLines() const
485{
dfc54541
JS
486 // HIDEOUSLY inefficient, but we have no choice.
487 char *s = XmTextGetString ((Widget) m_mainWidget);
488 if (s)
489 {
2d120f83
JS
490 long i = 0;
491 int currentLine = 0;
492 bool finished = FALSE;
493 while (!finished)
494 {
495 int ch = s[i];
496 if (ch == '\n')
497 {
498 currentLine++;
499 i++;
500 }
501 else if (ch == 0)
502 {
503 finished = TRUE;
504 }
505 else
506 i++;
507 }
66a007fb 508
2d120f83
JS
509 XtFree (s);
510 return currentLine;
dfc54541 511 }
4bb6408c
JS
512 return 0;
513}
514
515long wxTextCtrl::XYToPosition(long x, long y) const
516{
dfc54541 517/* It seems, that there is a bug in some versions of the Motif library,
2d120f83
JS
518 so the original wxWin-Code doesn't work. */
519 /*
520 Widget textWidget = (Widget) handle;
521 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
522 */
dfc54541
JS
523 /* Now a little workaround: */
524 long r=0;
525 for (int i=0; i<y; i++) r+=(GetLineLength(i)+1);
66a007fb 526 return r+x;
4bb6408c
JS
527}
528
529void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
530{
dfc54541
JS
531 Position xx, yy;
532 XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy);
533 *x = xx; *y = yy;
4bb6408c
JS
534}
535
536void wxTextCtrl::ShowPosition(long pos)
537{
dfc54541 538 XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
539}
540
541int wxTextCtrl::GetLineLength(long lineNo) const
542{
dfc54541
JS
543 wxString str = GetLineText (lineNo);
544 return (int) str.Length();
4bb6408c
JS
545}
546
547wxString wxTextCtrl::GetLineText(long lineNo) const
548{
dfc54541
JS
549 // HIDEOUSLY inefficient, but we have no choice.
550 char *s = XmTextGetString ((Widget) m_mainWidget);
66a007fb 551
dfc54541
JS
552 if (s)
553 {
554 wxString buf("");
555 long i;
556 int currentLine = 0;
557 for (i = 0; currentLine != lineNo && s[i]; i++ )
2d120f83
JS
558 if (s[i] == '\n')
559 currentLine++;
560 // Now get the text
561 int j;
562 for (j = 0; s[i] && s[i] != '\n'; i++, j++ )
563 buf += s[i];
66a007fb 564
2d120f83
JS
565 XtFree(s);
566 return buf;
567 }
568 else
569 return wxEmptyString;
4bb6408c
JS
570}
571
572/*
2d120f83
JS
573* Text item
574*/
575
4bb6408c
JS
576void wxTextCtrl::Command(wxCommandEvent & event)
577{
578 SetValue (event.GetString());
579 ProcessCommand (event);
580}
581
582void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
583{
584 // By default, load the first file into the text window.
585 if (event.GetNumberOfFiles() > 0)
586 {
587 LoadFile(event.GetFiles()[0]);
588 }
589}
590
591// The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
592// AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
593
594//=========================================================================
66a007fb 595// Called then the buffer is full (gcc 2.6.3)
4bb6408c
JS
596// or when "endl" is output (Borland 4.5)
597//=========================================================================
598// Class declaration using multiple inheritance doesn't work properly for
599// Borland. See note in wb_text.h.
600#ifndef NO_TEXT_WINDOW_STREAM
601int wxTextCtrl::overflow(int c)
602{
2d120f83
JS
603 // Make sure there is a holding area
604 if ( allocate()==EOF )
605 {
606 wxError("Streambuf allocation failed","Internal error");
607 return EOF;
608 }
66a007fb 609
2d120f83
JS
610 // Verify that there are no characters in get area
611 if ( gptr() && gptr() < egptr() )
612 {
613 wxError("wxTextCtrl::overflow: Who's trespassing my get area?","Internal error");
614 return EOF;
615 }
66a007fb 616
2d120f83
JS
617 // Reset get area
618 setg(0,0,0);
66a007fb 619
2d120f83
JS
620 // Make sure there is a put area
621 if ( ! pptr() )
622 {
623 /* This doesn't seem to be fatal so comment out error message */
624 // wxError("Put area not opened","Internal error");
625 setp( base(), base() );
626 }
66a007fb 627
2d120f83
JS
628 // Determine how many characters have been inserted but no consumed
629 int plen = pptr() - pbase();
66a007fb 630
2d120f83
JS
631 // Now Jerry relies on the fact that the buffer is at least 2 chars
632 // long, but the holding area "may be as small as 1" ???
633 // And we need an additional \0, so let's keep this inefficient but
634 // safe copy.
66a007fb 635
2d120f83
JS
636 // If c!=EOF, it is a character that must also be comsumed
637 int xtra = c==EOF? 0 : 1;
66a007fb 638
2d120f83
JS
639 // Write temporary C-string to wxTextWindow
640 {
641 char *txt = new char[plen+xtra+1];
642 memcpy(txt, pbase(), plen);
643 txt[plen] = (char)c; // append c
644 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
645 // If the put area already contained \0, output will be truncated there
646 WriteText(txt);
647 delete[] txt;
648 }
66a007fb 649
2d120f83
JS
650 // Reset put area
651 setp(pbase(), epptr());
66a007fb 652
4bb6408c 653#if defined(__WATCOMC__)
2d120f83 654 return __NOT_EOF;
4bb6408c 655#elif defined(zapeof) // HP-UX (all cfront based?)
2d120f83 656 return zapeof(c);
4bb6408c 657#else
2d120f83 658 return c!=EOF ? c : 0; // this should make everybody happy
4bb6408c
JS
659#endif
660}
661
662//=========================================================================
663// called then "endl" is output (gcc) or then explicit sync is done (Borland)
664//=========================================================================
665int wxTextCtrl::sync()
666{
2d120f83
JS
667 // Verify that there are no characters in get area
668 if ( gptr() && gptr() < egptr() )
669 {
670 wxError("Who's trespassing my get area?","Internal error");
671 return EOF;
672 }
66a007fb 673
2d120f83 674 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
66a007fb 675
2d120f83
JS
676 return 0;
677 /* OLD CODE
678 int len = pptr() - pbase();
679 char *txt = new char[len+1];
680 strncpy(txt, pbase(), len);
681 txt[len] = '\0';
682 (*this) << txt;
683 setp(pbase(), epptr());
684 delete[] txt;
685 return 0;
686 */
4bb6408c
JS
687}
688
689//=========================================================================
690// Should not be called by a "ostream". Used by a "istream"
691//=========================================================================
692int wxTextCtrl::underflow()
693{
2d120f83 694 return EOF;
4bb6408c
JS
695}
696#endif
697
698wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
699{
4dba84be 700 AppendText(s);
4bb6408c
JS
701 return *this;
702}
703
704wxTextCtrl& wxTextCtrl::operator<<(float f)
705{
706 wxString str;
707 str.Printf("%.2f", f);
4dba84be 708 AppendText(str);
4bb6408c
JS
709 return *this;
710}
711
712wxTextCtrl& wxTextCtrl::operator<<(double d)
713{
714 wxString str;
715 str.Printf("%.2f", d);
4dba84be 716 AppendText(str);
4bb6408c
JS
717 return *this;
718}
719
720wxTextCtrl& wxTextCtrl::operator<<(int i)
721{
722 wxString str;
723 str.Printf("%d", i);
4dba84be 724 AppendText(str);
4bb6408c
JS
725 return *this;
726}
727
728wxTextCtrl& wxTextCtrl::operator<<(long i)
729{
730 wxString str;
731 str.Printf("%ld", i);
4dba84be 732 AppendText(str);
4bb6408c
JS
733 return *this;
734}
735
736wxTextCtrl& wxTextCtrl::operator<<(const char c)
737{
738 char buf[2];
66a007fb 739
4bb6408c
JS
740 buf[0] = c;
741 buf[1] = 0;
4dba84be 742 AppendText(buf);
4bb6408c
JS
743 return *this;
744}
745
02e8b2f9
JS
746void wxTextCtrl::OnChar(wxKeyEvent& event)
747{
2d120f83
JS
748 // Indicates that we should generate a normal command, because
749 // we're letting default behaviour happen (otherwise it's vetoed
750 // by virtue of overriding OnChar)
751 m_processedDefault = TRUE;
66a007fb 752
2d120f83 753 if (m_tempCallbackStruct)
02e8b2f9 754 {
2d120f83
JS
755 XmTextVerifyCallbackStruct *textStruct =
756 (XmTextVerifyCallbackStruct *) m_tempCallbackStruct;
757 textStruct->doit = True;
758 if (isascii(event.m_keyCode) && (textStruct->text->length == 1))
759 {
760 textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode);
761 }
02e8b2f9 762 }
02e8b2f9
JS
763}
764
4b5f3fe6 765void wxTextCtrl::ChangeFont(bool keepOriginalSize)
0d57be45 766{
4b5f3fe6 767 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
768}
769
770void wxTextCtrl::ChangeBackgroundColour()
771{
321db4b6 772 wxWindow::ChangeBackgroundColour();
66a007fb 773
94b49b93 774 /* TODO: should scrollbars be affected? Should probably have separate
2d120f83
JS
775 * function to change them (by default, taken from wxSystemSettings)
776 */
94b49b93
JS
777 if (m_windowStyle & wxTE_MULTILINE)
778 {
779 Widget parent = XtParent ((Widget) m_mainWidget);
780 Widget hsb, vsb;
66a007fb 781
94b49b93 782 XtVaGetValues (parent,
2d120f83
JS
783 XmNhorizontalScrollBar, &hsb,
784 XmNverticalScrollBar, &vsb,
785 NULL);
94b49b93
JS
786 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
787 if (hsb)
788 DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
789 if (vsb)
790 DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
66a007fb 791
94b49b93
JS
792 DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE);
793 }
0d57be45
JS
794}
795
796void wxTextCtrl::ChangeForegroundColour()
797{
321db4b6 798 wxWindow::ChangeForegroundColour();
66a007fb 799
94b49b93
JS
800 if (m_windowStyle & wxTE_MULTILINE)
801 {
802 Widget parent = XtParent ((Widget) m_mainWidget);
803 Widget hsb, vsb;
66a007fb 804
94b49b93 805 XtVaGetValues (parent,
2d120f83
JS
806 XmNhorizontalScrollBar, &hsb,
807 XmNverticalScrollBar, &vsb,
808 NULL);
66a007fb 809
2d120f83
JS
810 /* TODO: should scrollbars be affected? Should probably have separate
811 * function to change them (by default, taken from wxSystemSettings)
812 if (hsb)
94b49b93 813 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
2d120f83 814 if (vsb)
94b49b93 815 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
2d120f83 816 */
94b49b93
JS
817 DoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
818 }
0d57be45
JS
819}
820
c27eab7e
VZ
821void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode)
822{
823 // we're in process of updating the text control
824 m_tempCallbackStruct = wxcbs;
825
826 XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs;
827
828 wxKeyEvent event (wxEVT_CHAR);
829 event.SetId(GetId());
830 event.m_keyCode = keycode;
831 event.SetEventObject(this);
832
833 // Only if wxTextCtrl::OnChar is called will this be set to True (and
834 // the character passed through)
835 cbs->doit = False;
836
837 GetEventHandler()->ProcessEvent(event);
838
839 if ( !InSetValue() && m_processedDefault )
840 {
841 // Can generate a command
842 wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId());
843 commandEvent.SetEventObject(this);
844 ProcessCommand(commandEvent);
845 }
846
847 // do it after the (user) event handlers processed the events because
848 // otherwise GetValue() would return incorrect (not yet updated value)
849 m_tempCallbackStruct = NULL;
850}
851
852// ----------------------------------------------------------------------------
853// helpers and Motif callbacks
854// ----------------------------------------------------------------------------
855
856static void MergeChangesIntoString(wxString& value,
857 XmTextVerifyCallbackStruct *cbs)
858{
859 /* _sm_
860 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
861 * every event as a replace event. cbs->text->ptr gives the replacement
862 * text, cbs->startPos gives the index of the first char affected by the
863 * replace, and cbs->endPos gives the index one more than the last char
864 * affected by the replace (startPos == endPos implies an empty range).
865 * Hence, a deletion is represented by replacing all input text with a
866 * blank string ("", *not* NULL!). A simple insertion that does not
867 * overwrite any text has startPos == endPos.
868 */
869
870 if ( !value )
871 {
872 // easy case: the ol value was empty
873 value = cbs->text->ptr;
874 }
875 else
876 {
877 // merge the changes into the value
878 const char * const passwd = value;
879 int len = value.length();
880
881 len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL
882 len -= cbs->endPos - cbs->startPos; // - text from affected region.
883
884 char * newS = new char [len];
885 char * dest = newS,
886 * insert = cbs->text->ptr;
887
888 // Copy (old) text from passwd, up to the start posn of the change.
889 int i;
890 const char * p = passwd;
891 for (i = 0; i < cbs->startPos; ++i)
892 *dest++ = *p++;
893
894 // Copy the text to be inserted).
895 while (*insert)
896 *dest++ = *insert++;
897
898 // Finally, copy into newS any remaining text from passwd[endPos] on.
899 for (p = passwd + cbs->endPos; *p; )
900 *dest++ = *p++;
901 *dest = 0;
902
903 value = newS;
904
905 delete[] newS;
906 }
907}
908
909static void
910wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr)
02e8b2f9 911{
2d120f83
JS
912 if (!wxGetWindowFromTable(w))
913 // Widget has been deleted!
914 return;
66a007fb 915
2d120f83
JS
916 wxTextCtrl *tw = (wxTextCtrl *) clientData;
917 tw->SetModified(TRUE);
02e8b2f9
JS
918}
919
66a007fb 920static void
02e8b2f9
JS
921wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs)
922{
dfc54541
JS
923 wxTextCtrl *tw = (wxTextCtrl *) clientData;
924 tw->m_processedDefault = FALSE;
66a007fb 925
c27eab7e
VZ
926 // First, do some stuff if it's a password control: in this case, we need
927 // to store the string inside the class because GetValue() can't retrieve
928 // it from the text ctrl. We do *not* do it in other circumstances because
929 // it would double the amount of memory needed.
66a007fb 930
c27eab7e 931 if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD )
dfc54541 932 {
c27eab7e 933 MergeChangesIntoString(tw->m_value, cbs);
66a007fb 934
c27eab7e 935 if ( cbs->text->length > 0 )
dfc54541
JS
936 {
937 int i;
938 for (i = 0; i < cbs->text->length; ++i)
939 cbs->text->ptr[i] = '*';
c27eab7e 940 cbs->text->ptr[i] = '\0';
dfc54541
JS
941 }
942 }
66a007fb 943
c27eab7e
VZ
944 // If we're already within an OnChar, return: probably a programmatic
945 // insertion.
dfc54541
JS
946 if (tw->m_tempCallbackStruct)
947 return;
66a007fb 948
dfc54541
JS
949 // Check for a backspace
950 if (cbs->startPos == (cbs->currInsert - 1))
951 {
c27eab7e 952 tw->DoSendEvents((void *)cbs, WXK_DELETE);
66a007fb 953
dfc54541
JS
954 return;
955 }
66a007fb 956
c27eab7e 957 // Pasting operation: let it through without calling OnChar
dfc54541
JS
958 if (cbs->text->length > 1)
959 return;
66a007fb 960
dfc54541
JS
961 // Something other than text
962 if (cbs->text->ptr == NULL)
963 return;
66a007fb 964
c27eab7e
VZ
965 // normal key press
966 char ch = cbs->text->ptr[0];
967 tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch);
02e8b2f9
JS
968}
969
66a007fb 970static void
02e8b2f9
JS
971wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs)
972{
2d120f83
JS
973 if (!wxGetWindowFromTable(w))
974 return;
66a007fb 975
2d120f83
JS
976 wxTextCtrl *tw = (wxTextCtrl *) clientData;
977 wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId());
978 event.SetEventObject(tw);
979 tw->GetEventHandler()->ProcessEvent(event);
02e8b2f9
JS
980}
981
66a007fb 982static void
02e8b2f9
JS
983wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs)
984{
2d120f83
JS
985 if (!wxGetWindowFromTable(w))
986 return;
66a007fb 987
2d120f83
JS
988 wxTextCtrl *tw = (wxTextCtrl *) clientData;
989 wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId());
990 event.SetEventObject(tw);
991 tw->GetEventHandler()->ProcessEvent(event);
02e8b2f9 992}
dfc54541
JS
993
994static void wxTextWindowActivateProc(Widget w, XtPointer clientData,
2d120f83 995 XmAnyCallbackStruct *ptr)
dfc54541 996{
2d120f83
JS
997 if (!wxGetWindowFromTable(w))
998 return;
66a007fb 999
2d120f83 1000 wxTextCtrl *tw = (wxTextCtrl *) clientData;
66a007fb 1001
2d120f83
JS
1002 if (tw->InSetValue())
1003 return;
66a007fb 1004
2d120f83
JS
1005 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER);
1006 event.SetId(tw->GetId());
1007 event.SetEventObject(tw);
1008 tw->ProcessCommand(event);
dfc54541 1009}
c27eab7e 1010
e702ff0f
JS
1011void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1012{
1013 Cut();
1014}
1015
1016void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1017{
1018 Copy();
1019}
1020
1021void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1022{
1023 Paste();
1024}
1025
1026void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1027{
1028 Undo();
1029}
1030
1031void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1032{
1033 Redo();
1034}
1035
1036void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1037{
1038 event.Enable( CanCut() );
1039}
1040
1041void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1042{
1043 event.Enable( CanCopy() );
1044}
1045
1046void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1047{
1048 event.Enable( CanPaste() );
1049}
1050
1051void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1052{
1053 event.Enable( CanUndo() );
1054}
1055
1056void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1057{
1058 event.Enable( CanRedo() );
1059}