implemented EditLabel() with custom text controls and GetEditControl()
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37708 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -138,8 +138,10 @@ public:
|
|||||||
|
|
||||||
void SetItemCount(long count);
|
void SetItemCount(long count);
|
||||||
|
|
||||||
void EditLabel( long item ) { Edit(item); }
|
wxTextCtrl *EditLabel(long item,
|
||||||
void Edit( long item );
|
wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl));
|
||||||
|
wxTextCtrl* GetEditControl() const;
|
||||||
|
void Edit( long item ) { EditLabel(item); }
|
||||||
|
|
||||||
bool EnsureVisible( long item );
|
bool EnsureVisible( long item );
|
||||||
long FindItem( long start, const wxString& str, bool partial = false );
|
long FindItem( long start, const wxString& str, bool partial = false );
|
||||||
|
@@ -471,13 +471,18 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// wxListTextCtrl (internal)
|
// wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
class WXDLLEXPORT wxListTextCtrl: public wxTextCtrl
|
class WXDLLEXPORT wxListTextCtrlWrapper : public wxEvtHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxListTextCtrl(wxListMainWindow *owner, size_t itemEdit);
|
// NB: text must be a valid object but not Create()d yet
|
||||||
|
wxListTextCtrlWrapper(wxListMainWindow *owner,
|
||||||
|
wxTextCtrl *text,
|
||||||
|
size_t itemEdit);
|
||||||
|
|
||||||
|
wxTextCtrl *GetText() const { return m_text; }
|
||||||
|
|
||||||
void AcceptChangesAndFinish();
|
void AcceptChangesAndFinish();
|
||||||
|
|
||||||
@@ -491,6 +496,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
wxListMainWindow *m_owner;
|
wxListMainWindow *m_owner;
|
||||||
|
wxTextCtrl *m_text;
|
||||||
wxString m_startValue;
|
wxString m_startValue;
|
||||||
size_t m_itemEdited;
|
size_t m_itemEdited;
|
||||||
bool m_finished;
|
bool m_finished;
|
||||||
@@ -598,7 +604,19 @@ public:
|
|||||||
void MoveToFocus() { MoveToItem(m_current); }
|
void MoveToFocus() { MoveToItem(m_current); }
|
||||||
|
|
||||||
// start editing the label of the given item
|
// start editing the label of the given item
|
||||||
void EditLabel( long item );
|
wxTextCtrl *EditLabel(long item,
|
||||||
|
wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl));
|
||||||
|
wxTextCtrl *GetEditControl() const
|
||||||
|
{
|
||||||
|
return m_textctrlWrapper ? m_textctrlWrapper->GetText() : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FinishEditing(wxTextCtrl *text)
|
||||||
|
{
|
||||||
|
delete text;
|
||||||
|
m_textctrlWrapper = NULL;
|
||||||
|
SetFocusIgnoringChildren();
|
||||||
|
}
|
||||||
|
|
||||||
// suspend/resume redrawing the control
|
// suspend/resume redrawing the control
|
||||||
void Freeze();
|
void Freeze();
|
||||||
@@ -775,8 +793,6 @@ public:
|
|||||||
m_lineBeforeLastClicked,
|
m_lineBeforeLastClicked,
|
||||||
m_lineSelectSingleOnUp;
|
m_lineSelectSingleOnUp;
|
||||||
|
|
||||||
wxListTextCtrl* m_textctrl;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
|
wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
|
||||||
|
|
||||||
@@ -849,6 +865,11 @@ private:
|
|||||||
// if this is > 0, the control is frozen and doesn't redraw itself
|
// if this is > 0, the control is frozen and doesn't redraw itself
|
||||||
size_t m_freezeCount;
|
size_t m_freezeCount;
|
||||||
|
|
||||||
|
// wrapper around the text control currently used for in place editing or
|
||||||
|
// NULL if no item is being edited
|
||||||
|
wxListTextCtrlWrapper *m_textctrlWrapper;
|
||||||
|
|
||||||
|
|
||||||
DECLARE_DYNAMIC_CLASS(wxListMainWindow)
|
DECLARE_DYNAMIC_CLASS(wxListMainWindow)
|
||||||
DECLARE_EVENT_TABLE()
|
DECLARE_EVENT_TABLE()
|
||||||
|
|
||||||
@@ -1977,20 +1998,23 @@ void wxListRenameTimer::Notify()
|
|||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// wxListTextCtrl (internal)
|
// wxListTextCtrlWrapper (internal)
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
BEGIN_EVENT_TABLE(wxListTextCtrl,wxTextCtrl)
|
BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
|
||||||
EVT_CHAR (wxListTextCtrl::OnChar)
|
EVT_CHAR (wxListTextCtrlWrapper::OnChar)
|
||||||
EVT_KEY_UP (wxListTextCtrl::OnKeyUp)
|
EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
|
||||||
EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus)
|
EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
wxListTextCtrl::wxListTextCtrl(wxListMainWindow *owner, size_t itemEdit)
|
wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
|
||||||
|
wxTextCtrl *text,
|
||||||
|
size_t itemEdit)
|
||||||
: m_startValue(owner->GetItemText(itemEdit)),
|
: m_startValue(owner->GetItemText(itemEdit)),
|
||||||
m_itemEdited(itemEdit)
|
m_itemEdited(itemEdit)
|
||||||
{
|
{
|
||||||
m_owner = owner;
|
m_owner = owner;
|
||||||
|
m_text = text;
|
||||||
m_finished = false;
|
m_finished = false;
|
||||||
m_aboutToFinish = false;
|
m_aboutToFinish = false;
|
||||||
|
|
||||||
@@ -1999,27 +2023,28 @@ wxListTextCtrl::wxListTextCtrl(wxListMainWindow *owner, size_t itemEdit)
|
|||||||
m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
|
m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
|
||||||
&rectLabel.x, &rectLabel.y);
|
&rectLabel.x, &rectLabel.y);
|
||||||
|
|
||||||
(void)Create(owner, wxID_ANY, m_startValue,
|
m_text->Create(owner, wxID_ANY, m_startValue,
|
||||||
wxPoint(rectLabel.x-4,rectLabel.y-4),
|
wxPoint(rectLabel.x-4,rectLabel.y-4),
|
||||||
wxSize(rectLabel.width+11,rectLabel.height+8));
|
wxSize(rectLabel.width+11,rectLabel.height+8));
|
||||||
|
m_text->PushEventHandler(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListTextCtrl::Finish()
|
void wxListTextCtrlWrapper::Finish()
|
||||||
{
|
{
|
||||||
if ( !m_finished )
|
if ( !m_finished )
|
||||||
{
|
{
|
||||||
wxPendingDelete.Append(this);
|
|
||||||
m_owner->m_textctrl = NULL;
|
|
||||||
|
|
||||||
m_finished = true;
|
m_finished = true;
|
||||||
|
|
||||||
m_owner->SetFocusIgnoringChildren();
|
m_text->PopEventHandler(this);
|
||||||
|
m_owner->FinishEditing(m_text);
|
||||||
|
|
||||||
|
delete this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxListTextCtrl::AcceptChanges()
|
bool wxListTextCtrlWrapper::AcceptChanges()
|
||||||
{
|
{
|
||||||
const wxString value = GetValue();
|
const wxString value = m_text->GetValue();
|
||||||
|
|
||||||
if ( value == m_startValue )
|
if ( value == m_startValue )
|
||||||
// nothing changed, always accept
|
// nothing changed, always accept
|
||||||
@@ -2035,7 +2060,7 @@ bool wxListTextCtrl::AcceptChanges()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListTextCtrl::AcceptChangesAndFinish()
|
void wxListTextCtrlWrapper::AcceptChangesAndFinish()
|
||||||
{
|
{
|
||||||
m_aboutToFinish = true;
|
m_aboutToFinish = true;
|
||||||
|
|
||||||
@@ -2046,7 +2071,7 @@ void wxListTextCtrl::AcceptChangesAndFinish()
|
|||||||
Finish();
|
Finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListTextCtrl::OnChar( wxKeyEvent &event )
|
void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
|
||||||
{
|
{
|
||||||
switch ( event.m_keyCode )
|
switch ( event.m_keyCode )
|
||||||
{
|
{
|
||||||
@@ -2064,7 +2089,7 @@ void wxListTextCtrl::OnChar( wxKeyEvent &event )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListTextCtrl::OnKeyUp( wxKeyEvent &event )
|
void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
|
||||||
{
|
{
|
||||||
if (m_finished)
|
if (m_finished)
|
||||||
{
|
{
|
||||||
@@ -2074,20 +2099,20 @@ void wxListTextCtrl::OnKeyUp( wxKeyEvent &event )
|
|||||||
|
|
||||||
// auto-grow the textctrl:
|
// auto-grow the textctrl:
|
||||||
wxSize parentSize = m_owner->GetSize();
|
wxSize parentSize = m_owner->GetSize();
|
||||||
wxPoint myPos = GetPosition();
|
wxPoint myPos = m_text->GetPosition();
|
||||||
wxSize mySize = GetSize();
|
wxSize mySize = m_text->GetSize();
|
||||||
int sx, sy;
|
int sx, sy;
|
||||||
GetTextExtent(GetValue() + _T("MM"), &sx, &sy);
|
m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
|
||||||
if (myPos.x + sx > parentSize.x)
|
if (myPos.x + sx > parentSize.x)
|
||||||
sx = parentSize.x - myPos.x;
|
sx = parentSize.x - myPos.x;
|
||||||
if (mySize.x > sx)
|
if (mySize.x > sx)
|
||||||
sx = mySize.x;
|
sx = mySize.x;
|
||||||
SetSize(sx, wxDefaultCoord);
|
m_text->SetSize(sx, wxDefaultCoord);
|
||||||
|
|
||||||
event.Skip();
|
event.Skip();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListTextCtrl::OnKillFocus( wxFocusEvent &event )
|
void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
|
||||||
{
|
{
|
||||||
if ( !m_finished && !m_aboutToFinish )
|
if ( !m_finished && !m_aboutToFinish )
|
||||||
{
|
{
|
||||||
@@ -2143,7 +2168,7 @@ void wxListMainWindow::Init()
|
|||||||
|
|
||||||
m_lastOnSame = false;
|
m_lastOnSame = false;
|
||||||
m_renameTimer = new wxListRenameTimer( this );
|
m_renameTimer = new wxListRenameTimer( this );
|
||||||
m_textctrl = NULL;
|
m_textctrlWrapper = NULL;
|
||||||
|
|
||||||
m_current =
|
m_current =
|
||||||
m_lineLastClicked =
|
m_lineLastClicked =
|
||||||
@@ -2751,31 +2776,37 @@ void wxListMainWindow::ChangeCurrent(size_t current)
|
|||||||
SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
|
SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListMainWindow::EditLabel( long item )
|
wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
|
||||||
{
|
{
|
||||||
wxCHECK_RET( (item >= 0) && ((size_t)item < GetItemCount()),
|
wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
|
||||||
wxT("wrong index in wxGenericListCtrl::EditLabel()") );
|
wxT("wrong index in wxGenericListCtrl::EditLabel()") );
|
||||||
|
|
||||||
|
wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
|
||||||
|
wxT("EditLabel() needs a text control") );
|
||||||
|
|
||||||
size_t itemEdit = (size_t)item;
|
size_t itemEdit = (size_t)item;
|
||||||
|
|
||||||
wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
|
wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
|
||||||
le.SetEventObject( GetParent() );
|
le.SetEventObject( GetParent() );
|
||||||
le.m_itemIndex = item;
|
le.m_itemIndex = item;
|
||||||
wxListLineData *data = GetLine(itemEdit);
|
wxListLineData *data = GetLine(itemEdit);
|
||||||
wxCHECK_RET( data, _T("invalid index in EditLabel()") );
|
wxCHECK_MSG( data, NULL, _T("invalid index in EditLabel()") );
|
||||||
data->GetItem( 0, le.m_item );
|
data->GetItem( 0, le.m_item );
|
||||||
|
|
||||||
if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
|
if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
|
||||||
|
{
|
||||||
// vetoed by user code
|
// vetoed by user code
|
||||||
return;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// We have to call this here because the label in question might just have
|
// We have to call this here because the label in question might just have
|
||||||
// been added and no screen update taken place.
|
// been added and no screen update taken place.
|
||||||
if ( m_dirty )
|
if ( m_dirty )
|
||||||
wxSafeYield();
|
wxSafeYield();
|
||||||
|
|
||||||
m_textctrl = new wxListTextCtrl(this, itemEdit);
|
wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
|
||||||
m_textctrl->SetFocus();
|
m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
|
||||||
|
return m_textctrlWrapper->GetText();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListMainWindow::OnRenameTimer()
|
void wxListMainWindow::OnRenameTimer()
|
||||||
@@ -2825,9 +2856,9 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event )
|
|||||||
// shutdown the edit control when the mouse is clicked elsewhere on the
|
// shutdown the edit control when the mouse is clicked elsewhere on the
|
||||||
// listctrl because the order of events is different (or something like
|
// listctrl because the order of events is different (or something like
|
||||||
// that), so explicitly end the edit if it is active.
|
// that), so explicitly end the edit if it is active.
|
||||||
if ( event.LeftDown() && m_textctrl)
|
if ( event.LeftDown() && m_textctrlWrapper )
|
||||||
m_textctrl->AcceptChangesAndFinish();
|
m_textctrlWrapper->AcceptChangesAndFinish();
|
||||||
#endif
|
#endif // __WXMAC__
|
||||||
|
|
||||||
event.SetEventObject( GetParent() );
|
event.SetEventObject( GetParent() );
|
||||||
if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
|
if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
|
||||||
@@ -5150,9 +5181,15 @@ bool wxGenericListCtrl::DeleteColumn( int col )
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxGenericListCtrl::Edit( long item )
|
wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
|
||||||
|
wxClassInfo* textControlClass)
|
||||||
{
|
{
|
||||||
m_mainWin->EditLabel( item );
|
return m_mainWin->EditLabel( item, textControlClass );
|
||||||
|
}
|
||||||
|
|
||||||
|
wxTextCtrl *wxGenericListCtrl::GetEditControl() const
|
||||||
|
{
|
||||||
|
return m_mainWin->GetEditControl();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxGenericListCtrl::EnsureVisible( long item )
|
bool wxGenericListCtrl::EnsureVisible( long item )
|
||||||
|
Reference in New Issue
Block a user