MyTextCtrl context menu is followed by a default pop menu

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
kathbeau
Knows some wx things
Knows some wx things
Posts: 48
Joined: Fri Nov 28, 2014 6:30 pm

MyTextCtrl context menu is followed by a default pop menu

Post by kathbeau » Thu Mar 14, 2019 8:57 pm

I have a wxScrolledWindow, with a wxBoxSizer.

I create a series of custom wxBoxSizer objects, each with multiple controls, and set these into the scrolled window sizer. Each of these custom objects represents a data item, including its structure, rules and current value.

One of the controls on the custom sizers is MyTextCtrl, which handles such things as changing font color when the text value is changed by the user.

I added a context menu to MyTextCtrl so that I can, among other things, revert the modified value back to its original state. All this is working as I want it to.

However, when I invoke the context menu, after my menu runs it is followed by what I presume to be a default context menu for wxTextCtrl (it contains items Undo, Cut, Copy, Paste, etc.).

Here is my code for MyTextCtrl:

Code: Select all

// in the .h

class TXMLDataForm;
class TItemDef;

enum 
{ 
	POP_RESTORE = 3000,
	POP_MOVE_TO_TOP,
	POP_SHOW_DEF
};

class MyTextCtrl : public wxTextCtrl
{
private:
	TXMLDataForm *FDataForm;
	TItemDef     *FItemDef;
public:
	MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value,
		const wxPoint &pos, const wxSize &size, int style = 0)
		: wxTextCtrl(parent, id, value, pos, size, style)
	{
		FDataForm = NULL;
		FItemDef = NULL;
	}

	void SetDataForm(TXMLDataForm* data_form) { FDataForm = data_form; }
	void SetItemDef(TItemDef* item_def) { FItemDef = item_def; }

	void OnExitTextCtrl(wxFocusEvent& event); // for FValueText on DisplayDataSizer
	void OnPopupMenu(wxContextMenuEvent& event);
	void OnEvent(wxMouseEvent& event);

	void OnRestoreOriginal(wxCommandEvent& event);


	wxDECLARE_EVENT_TABLE();
};

// in the .cpp
wxBEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
	EVT_CONTEXT_MENU(MyTextCtrl::OnPopupMenu)
	EVT_MOUSE_EVENTS(MyTextCtrl::OnEvent)
	EVT_MENU(POP_RESTORE, MyTextCtrl::OnRestoreOriginal)  // handler for pop menu option 1
wxEND_EVENT_TABLE()

void MyTextCtrl::OnPopupMenu(wxContextMenuEvent& event)
{
	wxMenu menu;
	menu.Append(POP_RESTORE, "&Restore original value");
	menu.Append(POP_MOVE_TO_TOP, "Move item to top of display");
	menu.Append(POP_SHOW_DEF, "Show dictionary definition");
	PopupMenu(&menu, ScreenToClient(event.GetPosition()));

	event.Skip();
}

void MyTextCtrl::OnEvent(wxMouseEvent& event)
{
	event.Skip();
}

void MyTextCtrl::OnRestoreOriginal(wxCommandEvent& event)
{
	wxString t_value = this->GetValue();
	if (FDataForm->RestoreOriginalValue(FItemDef, t_value))
	{
		this->ChangeValue(t_value);
		this->SetForegroundColour(*wxBLACK);
	}

	event.Skip();
}

 
How can I make the default context menu not pop up after my custom one? I have seen messages here that talk about implementing a custom event handler (i.e., for EVT_MOUSE_EVENTS), but I can't figure out how to use that

This is wxWidgets 3.1.1 on Win10-64, working in MS Visual Studio 2017 Community.

Thanks,
Kathleen

User avatar
doublemax
Moderator
Moderator
Posts: 13897
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: MyTextCtrl context menu is followed by a default pop menu

Post by doublemax » Thu Mar 14, 2019 9:14 pm

Don't call event.Skip() in OnPopupMenu().

If you don't call it, it means that you have "consumed" the event and the event handling will stop there.
But calling it means that other event handlers for the same event will be called.

And yes, the naming of the method doesn't make very clear what it does.
Use the source, Luke!

kathbeau
Knows some wx things
Knows some wx things
Posts: 48
Joined: Fri Nov 28, 2014 6:30 pm

Re: MyTextCtrl context menu is followed by a default pop menu

Post by kathbeau » Thu Mar 14, 2019 10:29 pm

Don't call event.Skip() in OnPopupMenu().
Oh good grief. I could have sworn I tried that. Thank you!

Kathleen

Post Reply