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