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