neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
context_menu.hpp
Go to the documentation of this file.
1// context_menu.hpp
2/*
3 neoGFX Design Studio
4 Copyright(C) 2020 Leigh Johnston
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
23#include <neogfx/app/i_app.hpp>
24#include <neogfx/app/action.hpp>
28
30{
31 inline void display_element_context_menu(i_widget& aParent, i_element& aElement)
32 {
33 context_menu menu{ aParent, aParent.root().mouse_position() + aParent.root().window_position() };
34 aElement.context_menu().trigger(menu.menu());
35 action actionSendToBack{ "Send To Back"_t };
36 action actionBringToFont{ "Bring To Front"_t };
37 auto& actionCut = service<i_app>().action_cut();
38 auto& actionCopy = service<i_app>().action_copy();
39 auto& actionPaste = service<i_app>().action_paste();
40 auto& actionDelete = service<i_app>().action_delete();
41 auto& actionSelectAll = service<i_app>().action_select_all();
42 if (aElement.has_layout_item() && aElement.layout_item().is_widget())
43 {
44 auto& caddyWidget = aElement.layout_item().as_widget().parent();
45 if (&*caddyWidget.parent().children().back() == &caddyWidget)
46 actionSendToBack.disable();
47 if (&*caddyWidget.parent().children().front() == &caddyWidget)
48 actionBringToFont.disable();
49 actionSendToBack.triggered([&]()
50 {
51 // todo: change order in element model
52 aElement.root().visit([&](i_element& aElement)
53 {
54 if (aElement.is_selected())
55 {
56 if (aElement.has_caddy())
57 aElement.caddy().send_to_back();
58 }
59 });
60 if (aElement.has_caddy())
61 aElement.caddy().children().front()->set_focus();
62 });
63 actionBringToFont.triggered([&]()
64 {
65 // todo: change order in element model
66 aElement.root().reverse_visit([&](i_element& aElement)
67 {
68 if (aElement.is_selected())
69 {
70 if (aElement.has_caddy())
71 aElement.caddy().bring_to_front();
72 }
73 });
74 });
75 menu.menu().add_action(actionSendToBack);
76 menu.menu().add_action(actionBringToFont);
77 menu.menu().add_separator();
78 }
79 menu.menu().add_action(actionCut);
80 menu.menu().add_action(actionCopy);
81 menu.menu().add_action(actionPaste);
82 menu.menu().add_action(actionDelete);
83 menu.menu().add_separator();
84 menu.menu().add_action(actionSelectAll);
85 menu.exec();
86 }
87
88}
virtual i_element_caddy & caddy() const =0
virtual i_layout_item & layout_item() const =0
virtual bool has_layout_item() const =0
void reverse_visit(std::function< void(i_element &)> aVisitor)
virtual bool is_selected() const =0
virtual i_element const & root() const =0
virtual bool has_caddy() const =0
virtual const i_widget & as_widget() const =0
virtual bool is_widget() const =0
virtual const widget_list & children() const =0
virtual const i_window & root() const =0
virtual const i_widget & parent() const =0
virtual point mouse_position() const =0
virtual point window_position() const =0
menu(i_menu &aParent, std::string const &aTitle=std::string{}, menu_type aType=menu_type::Popup)
void display_element_context_menu(i_widget &aParent, i_element &aElement)