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