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