EVT_TEXT_UPDATED bug fixed, text ctrl callbacks simplified

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1996 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-03-29 14:44:21 +00:00
parent dd59841c33
commit c27eab7e9c
2 changed files with 239 additions and 223 deletions

View File

@@ -6,22 +6,22 @@
// Created: 17/09/98 // Created: 17/09/98
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Julian Smart // Copyright: (c) Julian Smart
// Licence: wxWindows licence // Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#ifndef _WX_TEXTCTRL_H_ #ifndef _WX_TEXTCTRL_H_
#define _WX_TEXTCTRL_H_ #define _WX_TEXTCTRL_H_
#ifdef __GNUG__ #ifdef __GNUG__
#pragma interface "textctrl.h" #pragma interface "textctrl.h"
#endif #endif
#include "wx/ioswrap.h"
#include "wx/control.h" #include "wx/control.h"
#if wxUSE_IOSTREAMH // TODO Some platforms/compilers don't like inheritance from streambuf.
#include <iostream.h> #if (defined(__BORLANDC__) && !defined(__WIN32__)) || defined(__MWERKS__)
#else #define NO_TEXT_WINDOW_STREAM
#include <iostream>
#endif #endif
WXDLLEXPORT_DATA(extern const char*) wxTextCtrlNameStr; WXDLLEXPORT_DATA(extern const char*) wxTextCtrlNameStr;
@@ -29,121 +29,124 @@ WXDLLEXPORT_DATA(extern const char*) wxEmptyString;
// Single-line text item // Single-line text item
class WXDLLEXPORT wxTextCtrl: public wxControl class WXDLLEXPORT wxTextCtrl: public wxControl
// TODO Some platforms/compilers don't like inheritance from streambuf.
#if (defined(__BORLANDC__) && !defined(__WIN32__)) || defined(__MWERKS__)
#define NO_TEXT_WINDOW_STREAM
#endif
#ifndef NO_TEXT_WINDOW_STREAM #ifndef NO_TEXT_WINDOW_STREAM
, public streambuf , public streambuf
#endif #endif
{ {
DECLARE_DYNAMIC_CLASS(wxTextCtrl) DECLARE_DYNAMIC_CLASS(wxTextCtrl)
public: public:
// creation // creation
// -------- // --------
wxTextCtrl(); wxTextCtrl();
inline wxTextCtrl(wxWindow *parent, wxWindowID id, wxTextCtrl(wxWindow *parent,
const wxString& value = wxEmptyString, wxWindowID id,
const wxPoint& pos = wxDefaultPosition, const wxString& value = wxEmptyString,
const wxSize& size = wxDefaultSize, long style = 0, const wxPoint& pos = wxDefaultPosition,
const wxValidator& validator = wxDefaultValidator, const wxSize& size = wxDefaultSize,
const wxString& name = wxTextCtrlNameStr) long style = 0,
const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxTextCtrlNameStr)
#ifndef NO_TEXT_WINDOW_STREAM #ifndef NO_TEXT_WINDOW_STREAM
:streambuf() : streambuf()
#endif #endif
{ {
Create(parent, id, value, pos, size, style, validator, name); Create(parent, id, value, pos, size, style, validator, name);
} }
bool Create(wxWindow *parent, wxWindowID id, bool Create(wxWindow *parent, wxWindowID id,
const wxString& value = wxEmptyString, const wxString& value = wxEmptyString,
const wxPoint& pos = wxDefaultPosition, const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize, long style = 0, const wxSize& size = wxDefaultSize, long style = 0,
const wxValidator& validator = wxDefaultValidator, const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxTextCtrlNameStr); const wxString& name = wxTextCtrlNameStr);
// accessors // accessors
// --------- // ---------
virtual wxString GetValue() const ; virtual wxString GetValue() const;
virtual void SetValue(const wxString& value); virtual void SetValue(const wxString& value);
virtual int GetLineLength(long lineNo) const; virtual int GetLineLength(long lineNo) const;
virtual wxString GetLineText(long lineNo) const; virtual wxString GetLineText(long lineNo) const;
virtual int GetNumberOfLines() const; virtual int GetNumberOfLines() const;
// operations // operations
// ---------- // ----------
// Clipboard operations // Clipboard operations
virtual void Copy(); virtual void Copy();
virtual void Cut(); virtual void Cut();
virtual void Paste(); virtual void Paste();
virtual void SetInsertionPoint(long pos); virtual void SetInsertionPoint(long pos);
virtual void SetInsertionPointEnd(); virtual void SetInsertionPointEnd();
virtual long GetInsertionPoint() const ; virtual long GetInsertionPoint() const;
virtual long GetLastPosition() const ; virtual long GetLastPosition() const;
virtual void Replace(long from, long to, const wxString& value); virtual void Replace(long from, long to, const wxString& value);
virtual void Remove(long from, long to); virtual void Remove(long from, long to);
virtual void SetSelection(long from, long to); virtual void SetSelection(long from, long to);
virtual void SetEditable(bool editable); virtual void SetEditable(bool editable);
// streambuf implementation // streambuf implementation
#ifndef NO_TEXT_WINDOW_STREAM #ifndef NO_TEXT_WINDOW_STREAM
int overflow(int i); int overflow(int i);
int sync(); int sync();
int underflow(); int underflow();
#endif #endif
wxTextCtrl& operator<<(const wxString& s); wxTextCtrl& operator<<(const wxString& s);
wxTextCtrl& operator<<(int i); wxTextCtrl& operator<<(int i);
wxTextCtrl& operator<<(long i); wxTextCtrl& operator<<(long i);
wxTextCtrl& operator<<(float f); wxTextCtrl& operator<<(float f);
wxTextCtrl& operator<<(double d); wxTextCtrl& operator<<(double d);
wxTextCtrl& operator<<(const char c); wxTextCtrl& operator<<(const char c);
virtual bool LoadFile(const wxString& file); virtual bool LoadFile(const wxString& file);
virtual bool SaveFile(const wxString& file); virtual bool SaveFile(const wxString& file);
virtual void WriteText(const wxString& text); virtual void WriteText(const wxString& text);
virtual void AppendText(const wxString& text); virtual void AppendText(const wxString& text);
virtual void DiscardEdits(); virtual void DiscardEdits();
virtual bool IsModified() const; virtual bool IsModified() const;
virtual long XYToPosition(long x, long y) const ; virtual long XYToPosition(long x, long y) const;
virtual void PositionToXY(long pos, long *x, long *y) const ; virtual void PositionToXY(long pos, long *x, long *y) const;
virtual void ShowPosition(long pos); virtual void ShowPosition(long pos);
virtual void Clear(); virtual void Clear();
// callbacks // callbacks
// --------- // ---------
void OnDropFiles(wxDropFilesEvent& event); void OnDropFiles(wxDropFilesEvent& event);
void OnChar(wxKeyEvent& event); void OnChar(wxKeyEvent& event);
// void OnEraseBackground(wxEraseEvent& event); // void OnEraseBackground(wxEraseEvent& event);
virtual void Command(wxCommandEvent& event); virtual void Command(wxCommandEvent& event);
// Implementation // implementation from here to the end
virtual void ChangeFont(bool keepOriginalSize = TRUE); // -----------------------------------
virtual void ChangeBackgroundColour(); virtual void ChangeFont(bool keepOriginalSize = TRUE);
virtual void ChangeForegroundColour(); virtual void ChangeBackgroundColour();
inline void SetModified(bool mod) { m_modified = mod; } virtual void ChangeForegroundColour();
virtual WXWidget GetTopWidget() const; void SetModified(bool mod) { m_modified = mod; }
virtual WXWidget GetTopWidget() const;
// send the CHAR and TEXT_UPDATED events
void DoSendEvents(void /* XmTextVerifyCallbackStruct */ *cbs,
long keycode);
protected: protected:
wxString m_fileName; wxString m_fileName;
public: public:
// Motif-specific // Motif-specific
void* m_tempCallbackStruct; void* m_tempCallbackStruct;
bool m_modified; bool m_modified;
wxString m_value; // Required for password text controls wxString m_value; // Required for password text controls
bool m_processedDefault; // Did we call wxTextCtrl::OnChar?
// If so, generate a command event. // Did we call wxTextCtrl::OnChar? If so, generate a command event.
DECLARE_EVENT_TABLE() bool m_processedDefault;
private:
DECLARE_EVENT_TABLE()
}; };
#endif #endif

View File

@@ -39,6 +39,11 @@
// private functions // private functions
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// helper: inserts the new text in the value of the text ctrl and returns the
// result in place
static void MergeChangesIntoString(wxString& value,
XmTextVerifyCallbackStruct *textStruct);
// callbacks // callbacks
static void wxTextWindowChangedProc(Widget w, XtPointer clientData, XtPointer ptr); static void wxTextWindowChangedProc(Widget w, XtPointer clientData, XtPointer ptr);
static void wxTextWindowModifyProc(Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs); static void wxTextWindowModifyProc(Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs);
@@ -185,22 +190,34 @@ WXWidget wxTextCtrl::GetTopWidget() const
wxString wxTextCtrl::GetValue() const wxString wxTextCtrl::GetValue() const
{ {
wxString str; // result
if (m_windowStyle & wxTE_PASSWORD) if (m_windowStyle & wxTE_PASSWORD)
return m_value; {
// the value is stored always in m_value because it can't be retrieved
// from the text control
str = m_value;
}
else else
{ {
// just get the string from Motif
char *s = XmTextGetString ((Widget) m_mainWidget); char *s = XmTextGetString ((Widget) m_mainWidget);
if (s) if ( s )
{ {
wxString str(s); str = s;
XtFree (s); XtFree (s);
return str;
} }
else //else: return empty string
if ( m_tempCallbackStruct )
{ {
return wxEmptyString; // the string in the control isn't yet updated, can't use it as is
MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *)
m_tempCallbackStruct);
} }
} }
return str;
} }
void wxTextCtrl::SetValue(const wxString& value) void wxTextCtrl::SetValue(const wxString& value)
@@ -727,7 +744,96 @@ void wxTextCtrl::ChangeForegroundColour()
} }
} }
static void wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr) void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode)
{
// we're in process of updating the text control
m_tempCallbackStruct = wxcbs;
XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs;
wxKeyEvent event (wxEVT_CHAR);
event.SetId(GetId());
event.m_keyCode = keycode;
event.SetEventObject(this);
// Only if wxTextCtrl::OnChar is called will this be set to True (and
// the character passed through)
cbs->doit = False;
GetEventHandler()->ProcessEvent(event);
if ( !InSetValue() && m_processedDefault )
{
// Can generate a command
wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId());
commandEvent.SetEventObject(this);
ProcessCommand(commandEvent);
}
// do it after the (user) event handlers processed the events because
// otherwise GetValue() would return incorrect (not yet updated value)
m_tempCallbackStruct = NULL;
}
// ----------------------------------------------------------------------------
// helpers and Motif callbacks
// ----------------------------------------------------------------------------
static void MergeChangesIntoString(wxString& value,
XmTextVerifyCallbackStruct *cbs)
{
/* _sm_
* At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
* every event as a replace event. cbs->text->ptr gives the replacement
* text, cbs->startPos gives the index of the first char affected by the
* replace, and cbs->endPos gives the index one more than the last char
* affected by the replace (startPos == endPos implies an empty range).
* Hence, a deletion is represented by replacing all input text with a
* blank string ("", *not* NULL!). A simple insertion that does not
* overwrite any text has startPos == endPos.
*/
if ( !value )
{
// easy case: the ol value was empty
value = cbs->text->ptr;
}
else
{
// merge the changes into the value
const char * const passwd = value;
int len = value.length();
len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL
len -= cbs->endPos - cbs->startPos; // - text from affected region.
char * newS = new char [len];
char * dest = newS,
* insert = cbs->text->ptr;
// Copy (old) text from passwd, up to the start posn of the change.
int i;
const char * p = passwd;
for (i = 0; i < cbs->startPos; ++i)
*dest++ = *p++;
// Copy the text to be inserted).
while (*insert)
*dest++ = *insert++;
// Finally, copy into newS any remaining text from passwd[endPos] on.
for (p = passwd + cbs->endPos; *p; )
*dest++ = *p++;
*dest = 0;
value = newS;
delete[] newS;
}
}
static void
wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr)
{ {
if (!wxGetWindowFromTable(w)) if (!wxGetWindowFromTable(w))
// Widget has been deleted! // Widget has been deleted!
@@ -743,105 +849,38 @@ wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStru
wxTextCtrl *tw = (wxTextCtrl *) clientData; wxTextCtrl *tw = (wxTextCtrl *) clientData;
tw->m_processedDefault = FALSE; tw->m_processedDefault = FALSE;
// First, do some stuff if it's a password control. // First, do some stuff if it's a password control: in this case, we need
// (What does this do exactly?) // to store the string inside the class because GetValue() can't retrieve
// it from the text ctrl. We do *not* do it in other circumstances because
// it would double the amount of memory needed.
if (tw->GetWindowStyleFlag() & wxTE_PASSWORD) if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD )
{ {
/* _sm_ MergeChangesIntoString(tw->m_value, cbs);
* At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
* every event as a replace event. cbs->text->ptr gives the replacement
* text, cbs->startPos gives the index of the first char affected by the
* replace, and cbs->endPos gives the index one more than the last char
* affected by the replace (startPos == endPos implies an empty range).
* Hence, a deletion is represented by replacing all input text with a
* blank string ("", *not* NULL!). A simple insertion that does not
* overwrite any text has startPos == endPos.
*/
if (tw->m_value.IsNull()) if ( cbs->text->length > 0 )
{
tw->m_value = cbs->text->ptr;
}
else
{
char * passwd = (char*) (const char*) tw->m_value; // Set up a more convenient alias.
int len = passwd ? strlen(passwd) : 0; // Enough room for old text
len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL
len -= cbs->endPos - cbs->startPos; // - text from affected region.
char * newS = new char [len];
char * p = passwd, * dest = newS, * insert = cbs->text->ptr;
// Copy (old) text from passwd, up to the start posn of the change.
int i;
for (i = 0; i < cbs->startPos; ++i)
*dest++ = *p++;
// Copy the text to be inserted).
while (*insert)
*dest++ = *insert++;
// Finally, copy into newS any remaining text from passwd[endPos] on.
for (p = passwd + cbs->endPos; *p; )
*dest++ = *p++;
*dest = 0;
tw->m_value = newS;
delete[] newS;
}
if (cbs->text->length>0)
{ {
int i; int i;
for (i = 0; i < cbs->text->length; ++i) for (i = 0; i < cbs->text->length; ++i)
cbs->text->ptr[i] = '*'; cbs->text->ptr[i] = '*';
cbs->text->ptr[i] = 0; cbs->text->ptr[i] = '\0';
} }
} }
// If we're already within an OnChar, return: probably // If we're already within an OnChar, return: probably a programmatic
// a programmatic insertion. // insertion.
if (tw->m_tempCallbackStruct) if (tw->m_tempCallbackStruct)
return; return;
// Check for a backspace // Check for a backspace
if (cbs->startPos == (cbs->currInsert - 1)) if (cbs->startPos == (cbs->currInsert - 1))
{ {
tw->m_tempCallbackStruct = (void*) cbs; tw->DoSendEvents((void *)cbs, WXK_DELETE);
wxKeyEvent event (wxEVT_CHAR);
event.SetId(tw->GetId());
event.m_keyCode = WXK_DELETE;
event.SetEventObject(tw);
// Only if wxTextCtrl::OnChar is called
// will this be set to True (and the character
// passed through)
cbs->doit = False;
tw->GetEventHandler()->ProcessEvent(event);
tw->m_tempCallbackStruct = NULL;
if (tw->InSetValue())
return;
if (tw->m_processedDefault)
{
// Can generate a command
wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, tw->GetId());
commandEvent.SetEventObject(tw);
tw->ProcessCommand(commandEvent);
}
return; return;
} }
// Pasting operation: let it through without // Pasting operation: let it through without calling OnChar
// calling OnChar
if (cbs->text->length > 1) if (cbs->text->length > 1)
return; return;
@@ -849,38 +888,11 @@ wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStru
if (cbs->text->ptr == NULL) if (cbs->text->ptr == NULL)
return; return;
tw->m_tempCallbackStruct = (void*) cbs; // normal key press
char ch = cbs->text->ptr[0];
wxKeyEvent event (wxEVT_CHAR); tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch);
event.SetId(tw->GetId());
event.SetEventObject(tw);
event.m_keyCode = (cbs->text->ptr[0] == 10 ? 13 : cbs->text->ptr[0]);
// Only if wxTextCtrl::OnChar is called
// will this be set to True (and the character
// passed through)
cbs->doit = False;
tw->GetEventHandler()->ProcessEvent(event);
tw->m_tempCallbackStruct = NULL;
if (tw->InSetValue())
return;
if (tw->m_processedDefault)
{
// Can generate a command
wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, tw->GetId());
commandEvent.SetEventObject(tw);
tw->ProcessCommand(commandEvent);
}
} }
// ----------------------------------------------------------------------------
// callbacks
// ----------------------------------------------------------------------------
static void static void
wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs) wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs)
{ {
@@ -921,3 +933,4 @@ static void wxTextWindowActivateProc(Widget w, XtPointer clientData,
event.SetEventObject(tw); event.SetEventObject(tw);
tw->ProcessCommand(event); tw->ProcessCommand(event);
} }