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