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

View File

@ -39,6 +39,11 @@
// 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
static void wxTextWindowChangedProc(Widget w, XtPointer clientData, XtPointer ptr);
static void wxTextWindowModifyProc(Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs);
@ -185,22 +190,34 @@ WXWidget wxTextCtrl::GetTopWidget() const
wxString wxTextCtrl::GetValue() const
{
wxString str; // result
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
{
// just get the string from Motif
char *s = XmTextGetString ((Widget) m_mainWidget);
if (s)
if ( s )
{
wxString str(s);
str = 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)
@ -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))
// Widget has been deleted!
@ -743,105 +849,38 @@ wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStru
wxTextCtrl *tw = (wxTextCtrl *) clientData;
tw->m_processedDefault = FALSE;
// First, do some stuff if it's a password control.
// (What does this do exactly?)
// First, do some stuff if it's a password control: in this case, we need
// 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_
* 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.
*/
MergeChangesIntoString(tw->m_value, cbs);
if (tw->m_value.IsNull())
{
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)
if ( cbs->text->length > 0 )
{
int i;
for (i = 0; i < cbs->text->length; ++i)
cbs->text->ptr[i] = '*';
cbs->text->ptr[i] = 0;
cbs->text->ptr[i] = '\0';
}
}
// If we're already within an OnChar, return: probably
// a programmatic insertion.
// If we're already within an OnChar, return: probably a programmatic
// insertion.
if (tw->m_tempCallbackStruct)
return;
// Check for a backspace
if (cbs->startPos == (cbs->currInsert - 1))
{
tw->m_tempCallbackStruct = (void*) cbs;
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);
}
tw->DoSendEvents((void *)cbs, WXK_DELETE);
return;
}
// Pasting operation: let it through without
// calling OnChar
// Pasting operation: let it through without calling OnChar
if (cbs->text->length > 1)
return;
@ -849,38 +888,11 @@ wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStru
if (cbs->text->ptr == NULL)
return;
tw->m_tempCallbackStruct = (void*) cbs;
wxKeyEvent event (wxEVT_CHAR);
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);
}
// normal key press
char ch = cbs->text->ptr[0];
tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch);
}
// ----------------------------------------------------------------------------
// callbacks
// ----------------------------------------------------------------------------
static void
wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs)
{
@ -921,3 +933,4 @@ static void wxTextWindowActivateProc(Widget w, XtPointer clientData,
event.SetEventObject(tw);
tw->ProcessCommand(event);
}