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