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