Background info: In Excel, when you perform cut on a selection of block, the values are not moved until you paste. After cut, if any event (not all but most) other than paste happens, the block is not moved.
I am trying to implement the same idea. I have 2 functions:
void MyFrame::OnCut(wxCommandEvent& evt);
void MyFrame::OnPaste(wxCommandEvent& evt);
Also following events are defined:
wxDEFINE_EVENT(CUT_EVENT, wxCommandEvent);
wxDEFINE_EVENT(PASTE_EVENT, wxCommandEvent);
The code in OnCut is structured in 2 blocks. 1st block should be performed when cut is called, 2nd block should be performed when Paste is called.
void MyFrame::OnCut(wxCommandEvent& evt)
{
if (evt.GetEventType() != PASTE_EVENT){
m_CutEventCalled = true;
//Some code here
}
if (evt.GetEventType() == PASTE_EVENT)
//Some code here
m_CutEventCalled = false;
}
}
void MyFrame::OnPaste(wxCommandEvent& evt)
{
//Some other code
if(m_CutEventCalled){
wxCommandEvent PasteEvent;
PasteEvent.SetEventType(PASTE_EVENT);
PasteEvent.SetEventObject(this);
Bind(PASTE_EVENT, &MyFrame::OnCut, this);
wxPostEvent(this, PasteEvent);
}
}
The cut and paste events can be triggered by keyboard such as Ctrl+X, by ContextMenu or from RibbonButtons.
This, so far, works well if Cut event is immediately followed by a paste event. However, I want m_CutEventCalled=false
when some other event happens between cut and paste, say user changed his/her mind and without pasting after cut instead triggered a copy event.
bool MyFrame::ProcessEvent(wxEvent & evt)
{
static wxEvent* s_lastEvent = NULL;
if (&evt == s_lastEvent)
return false;
int evtID = evt.GetId();
//if this part of the code exists the whole thing doesnt work
//otherwise it only works cut immediately followed by paste
if (evtID != ID_PASTE && evtID != ID_CUT)
m_CutEventCalled = false;
return wxMDIChildFrame::ProcessEvent(evt);
}
My idea was any event with IDs other than ID_PASTE and ID_CUT should reset the m_CutEventCalled=false
. However, so far no success. I assume it has something to do with ProcessEvent
.
In other words, how can I know that any
event other than paste event happened after the cut event. One solution would be that I can place m_CutEventCalled=false
in all other event handlers but that does not seem to be an elegant solution.
Any ideas appreciated.
This is a very strange approach to solving the problem. You absolutely don't need to interfere with the event handling because it doesn't matter at all whether the "paste" event immediately follows "cut" or not (in fact, spoiler: it never will, there will always be some intermediate events between any such events generated by the user).
You just need to remember the "cut" data in your OnCut()
and then copy and remove it, if copied successfully, in your OnPaste()
.