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