]> git.saurik.com Git - wxWidgets.git/blame - src/motif/textctrl.cpp
Removed dependency on wxTabView.
[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,
d3a80c92 133 wxConstCast(name.c_str(), char),
66a007fb
VZ
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 (
d3a80c92 146 wxConstCast(name.c_str(), char),
66a007fb
VZ
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,
d3a80c92 178 XmNvalue, wxConstCast(value.c_str(), char),
b86de524 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,
d3a80c92 253 XmNvalue, wxConstCast(value.c_str(), char),
b86de524 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,
d3a80c92 365 wxConstCast(value.c_str(), char));
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 396 struct stat statb;
d3a80c92
MB
397 if ((stat (wxConstCast(file.c_str(), char), &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
398 !(fp = fopen (wxConstCast(file.c_str(), char), "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
d3a80c92 439 if (!(fp = fopen (wxConstCast(theFile.c_str(), char), "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 465 long textPosition = GetInsertionPoint() + strlen (text);
d3a80c92
MB
466 XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(),
467 wxConstCast(text.c_str(), char));
dfc54541
JS
468 XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL);
469 SetInsertionPoint(textPosition);
470 XmTextShowPosition ((Widget) m_mainWidget, textPosition);
471 m_modified = TRUE;
4bb6408c
JS
472}
473
4dba84be
JS
474void wxTextCtrl::AppendText(const wxString& text)
475{
476 long textPosition = GetLastPosition() + strlen(text);
d3a80c92
MB
477 XmTextInsert ((Widget) m_mainWidget, GetLastPosition(),
478 wxConstCast(text.c_str(), char));
4dba84be
JS
479 XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL);
480 SetInsertionPoint(textPosition);
481 XmTextShowPosition ((Widget) m_mainWidget, textPosition);
482 m_modified = TRUE;
483}
484
4bb6408c
JS
485void wxTextCtrl::Clear()
486{
02e8b2f9 487 XmTextSetString ((Widget) m_mainWidget, "");
02e8b2f9 488 m_modified = FALSE;
4bb6408c
JS
489}
490
491bool wxTextCtrl::IsModified() const
492{
02e8b2f9 493 return m_modified;
4bb6408c
JS
494}
495
496// Makes 'unmodified'
497void wxTextCtrl::DiscardEdits()
498{
dfc54541 499 m_modified = FALSE;
4bb6408c
JS
500}
501
502int wxTextCtrl::GetNumberOfLines() const
503{
dfc54541
JS
504 // HIDEOUSLY inefficient, but we have no choice.
505 char *s = XmTextGetString ((Widget) m_mainWidget);
506 if (s)
507 {
2d120f83
JS
508 long i = 0;
509 int currentLine = 0;
510 bool finished = FALSE;
511 while (!finished)
512 {
513 int ch = s[i];
514 if (ch == '\n')
515 {
516 currentLine++;
517 i++;
518 }
519 else if (ch == 0)
520 {
521 finished = TRUE;
522 }
523 else
524 i++;
525 }
66a007fb 526
2d120f83
JS
527 XtFree (s);
528 return currentLine;
dfc54541 529 }
4bb6408c
JS
530 return 0;
531}
532
533long wxTextCtrl::XYToPosition(long x, long y) const
534{
dfc54541 535/* It seems, that there is a bug in some versions of the Motif library,
2d120f83
JS
536 so the original wxWin-Code doesn't work. */
537 /*
538 Widget textWidget = (Widget) handle;
539 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
540 */
dfc54541
JS
541 /* Now a little workaround: */
542 long r=0;
543 for (int i=0; i<y; i++) r+=(GetLineLength(i)+1);
66a007fb 544 return r+x;
4bb6408c
JS
545}
546
813c20a6 547bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
4bb6408c 548{
dfc54541
JS
549 Position xx, yy;
550 XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy);
813c20a6
VZ
551 if ( x )
552 *x = xx;
553 if ( y )
554 *y = yy;
555
556 return TRUE;
4bb6408c
JS
557}
558
559void wxTextCtrl::ShowPosition(long pos)
560{
dfc54541 561 XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
562}
563
564int wxTextCtrl::GetLineLength(long lineNo) const
565{
dfc54541
JS
566 wxString str = GetLineText (lineNo);
567 return (int) str.Length();
4bb6408c
JS
568}
569
570wxString wxTextCtrl::GetLineText(long lineNo) const
571{
dfc54541
JS
572 // HIDEOUSLY inefficient, but we have no choice.
573 char *s = XmTextGetString ((Widget) m_mainWidget);
66a007fb 574
dfc54541
JS
575 if (s)
576 {
577 wxString buf("");
578 long i;
579 int currentLine = 0;
580 for (i = 0; currentLine != lineNo && s[i]; i++ )
2d120f83
JS
581 if (s[i] == '\n')
582 currentLine++;
583 // Now get the text
584 int j;
585 for (j = 0; s[i] && s[i] != '\n'; i++, j++ )
586 buf += s[i];
66a007fb 587
2d120f83
JS
588 XtFree(s);
589 return buf;
590 }
591 else
592 return wxEmptyString;
4bb6408c
JS
593}
594
595/*
2d120f83
JS
596* Text item
597*/
598
4bb6408c
JS
599void wxTextCtrl::Command(wxCommandEvent & event)
600{
601 SetValue (event.GetString());
602 ProcessCommand (event);
603}
604
605void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
606{
607 // By default, load the first file into the text window.
608 if (event.GetNumberOfFiles() > 0)
609 {
610 LoadFile(event.GetFiles()[0]);
611 }
612}
613
02e8b2f9
JS
614void wxTextCtrl::OnChar(wxKeyEvent& event)
615{
2d120f83
JS
616 // Indicates that we should generate a normal command, because
617 // we're letting default behaviour happen (otherwise it's vetoed
618 // by virtue of overriding OnChar)
619 m_processedDefault = TRUE;
66a007fb 620
2d120f83 621 if (m_tempCallbackStruct)
02e8b2f9 622 {
2d120f83
JS
623 XmTextVerifyCallbackStruct *textStruct =
624 (XmTextVerifyCallbackStruct *) m_tempCallbackStruct;
625 textStruct->doit = True;
626 if (isascii(event.m_keyCode) && (textStruct->text->length == 1))
627 {
628 textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode);
629 }
02e8b2f9 630 }
02e8b2f9
JS
631}
632
4b5f3fe6 633void wxTextCtrl::ChangeFont(bool keepOriginalSize)
0d57be45 634{
4b5f3fe6 635 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
636}
637
638void wxTextCtrl::ChangeBackgroundColour()
639{
321db4b6 640 wxWindow::ChangeBackgroundColour();
66a007fb 641
94b49b93 642 /* TODO: should scrollbars be affected? Should probably have separate
2d120f83
JS
643 * function to change them (by default, taken from wxSystemSettings)
644 */
94b49b93
JS
645 if (m_windowStyle & wxTE_MULTILINE)
646 {
647 Widget parent = XtParent ((Widget) m_mainWidget);
648 Widget hsb, vsb;
66a007fb 649
94b49b93 650 XtVaGetValues (parent,
2d120f83
JS
651 XmNhorizontalScrollBar, &hsb,
652 XmNverticalScrollBar, &vsb,
653 NULL);
a756f210 654 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
94b49b93
JS
655 if (hsb)
656 DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
657 if (vsb)
658 DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
66a007fb 659
1119b899
MB
660 // MBN: why change parent background?
661 // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE);
94b49b93 662 }
0d57be45
JS
663}
664
665void wxTextCtrl::ChangeForegroundColour()
666{
321db4b6 667 wxWindow::ChangeForegroundColour();
66a007fb 668
94b49b93
JS
669 if (m_windowStyle & wxTE_MULTILINE)
670 {
671 Widget parent = XtParent ((Widget) m_mainWidget);
672 Widget hsb, vsb;
66a007fb 673
94b49b93 674 XtVaGetValues (parent,
2d120f83
JS
675 XmNhorizontalScrollBar, &hsb,
676 XmNverticalScrollBar, &vsb,
677 NULL);
66a007fb 678
2d120f83
JS
679 /* TODO: should scrollbars be affected? Should probably have separate
680 * function to change them (by default, taken from wxSystemSettings)
681 if (hsb)
94b49b93 682 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
2d120f83 683 if (vsb)
94b49b93 684 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
2d120f83 685 */
94b49b93
JS
686 DoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
687 }
0d57be45
JS
688}
689
c27eab7e
VZ
690void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode)
691{
692 // we're in process of updating the text control
693 m_tempCallbackStruct = wxcbs;
694
695 XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs;
696
697 wxKeyEvent event (wxEVT_CHAR);
698 event.SetId(GetId());
699 event.m_keyCode = keycode;
700 event.SetEventObject(this);
701
702 // Only if wxTextCtrl::OnChar is called will this be set to True (and
703 // the character passed through)
704 cbs->doit = False;
705
706 GetEventHandler()->ProcessEvent(event);
707
708 if ( !InSetValue() && m_processedDefault )
709 {
710 // Can generate a command
711 wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId());
712 commandEvent.SetEventObject(this);
713 ProcessCommand(commandEvent);
714 }
715
716 // do it after the (user) event handlers processed the events because
717 // otherwise GetValue() would return incorrect (not yet updated value)
718 m_tempCallbackStruct = NULL;
719}
720
e1aae528
MB
721wxSize wxDoGetSingleTextCtrlBestSize( Widget textWidget,
722 const wxWindow* window )
723{
724 Dimension xmargin, ymargin, highlight, shadow;
725 char* value;
726
727 XtVaGetValues( textWidget,
728 XmNmarginWidth, &xmargin,
729 XmNmarginHeight, &ymargin,
730 XmNvalue, &value,
731 XmNhighlightThickness, &highlight,
732 XmNshadowThickness, &shadow,
733 NULL );
1119b899 734
e1aae528
MB
735 if( !value )
736 value = "|";
737
738 int x, y;
739 window->GetTextExtent( value, &x, &y );
740
1119b899
MB
741 if( x < 100 ) x = 100;
742
e1aae528
MB
743 return wxSize( x + 2 * xmargin + 2 * highlight + 2 * shadow,
744 // MBN: +2 necessary: Lesstif bug or mine?
745 y + 2 * ymargin + 2 * highlight + 2 * shadow + 2 );
746}
747
748wxSize wxTextCtrl::DoGetBestSize() const
749{
750 if( IsSingleLine() )
751 return wxDoGetSingleTextCtrlBestSize( (Widget)m_mainWidget, this );
752 else
753 return wxWindow::DoGetBestSize();
754}
755
c27eab7e
VZ
756// ----------------------------------------------------------------------------
757// helpers and Motif callbacks
758// ----------------------------------------------------------------------------
759
760static void MergeChangesIntoString(wxString& value,
761 XmTextVerifyCallbackStruct *cbs)
762{
763 /* _sm_
764 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
765 * every event as a replace event. cbs->text->ptr gives the replacement
766 * text, cbs->startPos gives the index of the first char affected by the
767 * replace, and cbs->endPos gives the index one more than the last char
768 * affected by the replace (startPos == endPos implies an empty range).
769 * Hence, a deletion is represented by replacing all input text with a
770 * blank string ("", *not* NULL!). A simple insertion that does not
771 * overwrite any text has startPos == endPos.
772 */
773
774 if ( !value )
775 {
776 // easy case: the ol value was empty
777 value = cbs->text->ptr;
778 }
779 else
780 {
781 // merge the changes into the value
782 const char * const passwd = value;
783 int len = value.length();
784
44d130a3
MB
785 len += ( cbs->text->ptr ?
786 strlen(cbs->text->ptr) :
787 0 ) + 1; // + new text (if any) + NUL
c27eab7e
VZ
788 len -= cbs->endPos - cbs->startPos; // - text from affected region.
789
790 char * newS = new char [len];
791 char * dest = newS,
792 * insert = cbs->text->ptr;
793
794 // Copy (old) text from passwd, up to the start posn of the change.
795 int i;
796 const char * p = passwd;
797 for (i = 0; i < cbs->startPos; ++i)
798 *dest++ = *p++;
799
800 // Copy the text to be inserted).
44d130a3
MB
801 if (insert)
802 while (*insert)
803 *dest++ = *insert++;
c27eab7e
VZ
804
805 // Finally, copy into newS any remaining text from passwd[endPos] on.
806 for (p = passwd + cbs->endPos; *p; )
807 *dest++ = *p++;
808 *dest = 0;
809
810 value = newS;
811
812 delete[] newS;
813 }
814}
815
816static void
af111fc3 817wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 818{
2d120f83
JS
819 if (!wxGetWindowFromTable(w))
820 // Widget has been deleted!
821 return;
66a007fb 822
2d120f83
JS
823 wxTextCtrl *tw = (wxTextCtrl *) clientData;
824 tw->SetModified(TRUE);
02e8b2f9
JS
825}
826
66a007fb 827static void
af111fc3 828wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs)
02e8b2f9 829{
dfc54541
JS
830 wxTextCtrl *tw = (wxTextCtrl *) clientData;
831 tw->m_processedDefault = FALSE;
66a007fb 832
c27eab7e
VZ
833 // First, do some stuff if it's a password control: in this case, we need
834 // to store the string inside the class because GetValue() can't retrieve
835 // it from the text ctrl. We do *not* do it in other circumstances because
836 // it would double the amount of memory needed.
66a007fb 837
c27eab7e 838 if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD )
dfc54541 839 {
c27eab7e 840 MergeChangesIntoString(tw->m_value, cbs);
66a007fb 841
c27eab7e 842 if ( cbs->text->length > 0 )
dfc54541
JS
843 {
844 int i;
845 for (i = 0; i < cbs->text->length; ++i)
846 cbs->text->ptr[i] = '*';
c27eab7e 847 cbs->text->ptr[i] = '\0';
dfc54541
JS
848 }
849 }
66a007fb 850
c27eab7e
VZ
851 // If we're already within an OnChar, return: probably a programmatic
852 // insertion.
dfc54541
JS
853 if (tw->m_tempCallbackStruct)
854 return;
66a007fb 855
dfc54541
JS
856 // Check for a backspace
857 if (cbs->startPos == (cbs->currInsert - 1))
858 {
c27eab7e 859 tw->DoSendEvents((void *)cbs, WXK_DELETE);
66a007fb 860
dfc54541
JS
861 return;
862 }
66a007fb 863
c27eab7e 864 // Pasting operation: let it through without calling OnChar
dfc54541
JS
865 if (cbs->text->length > 1)
866 return;
66a007fb 867
dfc54541
JS
868 // Something other than text
869 if (cbs->text->ptr == NULL)
870 return;
66a007fb 871
c27eab7e
VZ
872 // normal key press
873 char ch = cbs->text->ptr[0];
874 tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch);
02e8b2f9
JS
875}
876
66a007fb 877static void
af111fc3 878wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs))
02e8b2f9 879{
2d120f83
JS
880 if (!wxGetWindowFromTable(w))
881 return;
66a007fb 882
2d120f83
JS
883 wxTextCtrl *tw = (wxTextCtrl *) clientData;
884 wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId());
885 event.SetEventObject(tw);
886 tw->GetEventHandler()->ProcessEvent(event);
02e8b2f9
JS
887}
888
66a007fb 889static void
af111fc3 890wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs))
02e8b2f9 891{
2d120f83
JS
892 if (!wxGetWindowFromTable(w))
893 return;
66a007fb 894
2d120f83
JS
895 wxTextCtrl *tw = (wxTextCtrl *) clientData;
896 wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId());
897 event.SetEventObject(tw);
898 tw->GetEventHandler()->ProcessEvent(event);
02e8b2f9 899}
dfc54541
JS
900
901static void wxTextWindowActivateProc(Widget w, XtPointer clientData,
af111fc3 902 XmAnyCallbackStruct *WXUNUSED(ptr))
dfc54541 903{
2d120f83
JS
904 if (!wxGetWindowFromTable(w))
905 return;
66a007fb 906
2d120f83 907 wxTextCtrl *tw = (wxTextCtrl *) clientData;
66a007fb 908
2d120f83
JS
909 if (tw->InSetValue())
910 return;
66a007fb 911
2d120f83
JS
912 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER);
913 event.SetId(tw->GetId());
914 event.SetEventObject(tw);
915 tw->ProcessCommand(event);
dfc54541 916}
c27eab7e 917
e702ff0f
JS
918void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
919{
920 Cut();
921}
922
923void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
924{
925 Copy();
926}
927
928void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
929{
930 Paste();
931}
932
933void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
934{
935 Undo();
936}
937
938void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
939{
940 Redo();
941}
942
943void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
944{
945 event.Enable( CanCut() );
946}
947
948void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
949{
950 event.Enable( CanCopy() );
951}
952
953void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
954{
955 event.Enable( CanPaste() );
956}
957
958void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
959{
960 event.Enable( CanUndo() );
961}
962
963void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
964{
965 event.Enable( CanRedo() );
966}