From 962979ebf14d20dbe8fcfa7e9b82cbd6b02fc54b Mon Sep 17 00:00:00 2001 From: New Pagodi Date: Thu, 16 Mar 2017 23:31:17 +0100 Subject: [PATCH] Add option to display custom context menu in stc sample This option could be useful to demonstrate ability to replace (override) standard Scintilla context menu with custom one. Switching between standard and custom popup menu is done with "Extra -> Custom popup menu" menu option. --- samples/stc/defsext.h | 1 + samples/stc/edit.cpp | 8 +++++++- samples/stc/edit.h | 1 + samples/stc/stctest.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/samples/stc/defsext.h b/samples/stc/defsext.h index d152c377eb..f046ecdbf2 100644 --- a/samples/stc/defsext.h +++ b/samples/stc/defsext.h @@ -76,6 +76,7 @@ enum { myID_MULTIPLE_SELECTIONS, myID_MULTI_PASTE, myID_MULTIPLE_SELECTIONS_TYPING, + myID_CUSTOM_POPUP, myID_USECHARSET, myID_CHARSETANSI, myID_CHARSETMAC, diff --git a/samples/stc/edit.cpp b/samples/stc/edit.cpp index 46b4154a3e..7ac43e73bb 100644 --- a/samples/stc/edit.cpp +++ b/samples/stc/edit.cpp @@ -111,6 +111,7 @@ wxBEGIN_EVENT_TABLE (Edit, wxStyledTextCtrl) EVT_MENU(myID_MULTIPLE_SELECTIONS, Edit::OnMultipleSelections) EVT_MENU(myID_MULTI_PASTE, Edit::OnMultiPaste) EVT_MENU(myID_MULTIPLE_SELECTIONS_TYPING, Edit::OnMultipleSelectionsTyping) + EVT_MENU(myID_CUSTOM_POPUP, Edit::OnCustomPopup) // stc EVT_STC_MARGINCLICK (wxID_ANY, Edit::OnMarginClick) EVT_STC_CHARADDED (wxID_ANY, Edit::OnCharAdded) @@ -175,7 +176,7 @@ Edit::Edit (wxWindow *parent, wxWindowID id, m_FoldingMargin = 16; CmdKeyClear (wxSTC_KEY_TAB, 0); // this is done by the menu accelerator key SetLayoutCache (wxSTC_CACHE_PAGE); - + UsePopUp(wxSTC_POPUP_ALL); } Edit::~Edit () {} @@ -453,6 +454,11 @@ void Edit::OnMultipleSelectionsTyping(wxCommandEvent& WXUNUSED(event)) { SetAdditionalSelectionTyping(!isSet); } +void Edit::OnCustomPopup(wxCommandEvent& evt) +{ + UsePopUp(evt.IsChecked() ? wxSTC_POPUP_NEVER : wxSTC_POPUP_ALL); +} + //! misc void Edit::OnMarginClick (wxStyledTextEvent &event) { if (event.GetMargin() == 2) { diff --git a/samples/stc/edit.h b/samples/stc/edit.h index 5613accc81..cf22973f12 100644 --- a/samples/stc/edit.h +++ b/samples/stc/edit.h @@ -101,6 +101,7 @@ public: void OnMultipleSelections(wxCommandEvent& event); void OnMultiPaste(wxCommandEvent& event); void OnMultipleSelectionsTyping(wxCommandEvent& event); + void OnCustomPopup(wxCommandEvent& evt); // stc void OnMarginClick (wxStyledTextEvent &event); void OnCharAdded (wxStyledTextEvent &event); diff --git a/samples/stc/stctest.cpp b/samples/stc/stctest.cpp index 41cf9c2cef..c7524bea2a 100644 --- a/samples/stc/stctest.cpp +++ b/samples/stc/stctest.cpp @@ -146,6 +146,7 @@ public: void OnPrint (wxCommandEvent &event); //! edit events void OnEdit (wxCommandEvent &event); + void OnContextMenu(wxContextMenuEvent& evt); private: // edit object @@ -276,6 +277,7 @@ wxBEGIN_EVENT_TABLE (AppFrame, wxFrame) AppFrame::OnEdit) // help EVT_MENU (wxID_ABOUT, AppFrame::OnAbout) + EVT_CONTEXT_MENU( AppFrame::OnContextMenu) wxEND_EVENT_TABLE () AppFrame::AppFrame (const wxString &title) @@ -438,6 +440,27 @@ void AppFrame::OnEdit (wxCommandEvent &event) { if (m_edit) m_edit->GetEventHandler()->ProcessEvent (event); } +void AppFrame::OnContextMenu(wxContextMenuEvent& evt) +{ + wxPoint point = evt.GetPosition(); + // If from keyboard + if ( point.x == -1 && point.y == -1 ) + { + wxSize size = GetSize(); + point.x = size.x / 2; + point.y = size.y / 2; + } + else + { + point = ScreenToClient(point); + } + + wxMenu menu; + menu.Append(wxID_ABOUT, wxT("&About")); + menu.Append(wxID_EXIT, wxT("E&xit")); + PopupMenu(&menu, point); +} + // private functions void AppFrame::CreateMenu () { @@ -549,6 +572,8 @@ void AppFrame::CreateMenu () menuExtra->AppendCheckItem(myID_MULTIPLE_SELECTIONS, _("Toggle &multiple selections")); menuExtra->AppendCheckItem(myID_MULTI_PASTE, _("Toggle multi-&paste")); menuExtra->AppendCheckItem(myID_MULTIPLE_SELECTIONS_TYPING, _("Toggle t&yping on multiple selections")); + menuExtra->AppendSeparator(); + menuExtra->AppendCheckItem (myID_CUSTOM_POPUP, _("C&ustom popup menu")); // Window menu wxMenu *menuWindow = new wxMenu;