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