swift
Loading...
Searching...
No Matches
menus.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include "menus.h"
7
8#include <algorithm>
9#include <cassert>
10#include <string>
11#include <type_traits>
12
13namespace XSwiftBus
14{
16 template <typename T>
17 void *voidptr_cast(T i)
18 {
19 static_assert(std::is_integral_v<T>, "voidptr_cast expects an integer");
20 using intptr_type = std::conditional_t<std::is_signed_v<T>, intptr_t, uintptr_t>;
21 return reinterpret_cast<void *>(static_cast<intptr_type>(i));
22 }
23
25 template <typename T>
26 T intptr_cast(void *p)
27 {
28 static_assert(std::is_integral<T>::value, "voidptr_cast returns an integer");
29 using intptr_type = std::conditional_t<std::is_signed_v<T>, intptr_t, uintptr_t>;
30 return static_cast<T>(reinterpret_cast<intptr_type>(p));
31 }
32
33 CMenu::CMenu(XPLMMenuID id, bool isMainMenu, std::unique_ptr<ItemList> items)
34 : m_data(std::make_shared<Data>(id, isMainMenu, std::move(items)))
35 {}
36
37 CMenu::Data::~Data()
38 {
39 if (!isMainMenu)
40 {
41 XPLMDestroyMenu(id);
42 }
43 }
44
45 CMenu CMenu::mainMenu() { return { XPLMFindPluginsMenu(), true, nullptr }; }
46
47 CMenuItem CMenu::appendItem(const std::string &name, std::function<void()> callback)
48 {
49 assert(!name.empty());
50 m_data->items->emplace_back(CMenuItem { m_data->id, 0, false, [callback](bool) { callback(); } });
51 auto &menuItem = m_data->items->back();
52 menuItem.setIndex(XPLMAppendMenuItem(m_data->id, name.c_str(), &menuItem, false));
53 return menuItem;
54 }
55
56 CMenuItem CMenu::checkableItem(const std::string &name, bool checked, std::function<void(bool)> callback)
57 {
58 assert(!name.empty());
59 m_data->items->emplace_back(CMenuItem { m_data->id, 0, true, callback });
60 auto &menuItem = m_data->items->back();
61 menuItem.setIndex(XPLMAppendMenuItem(m_data->id, name.c_str(), &menuItem, false));
62 menuItem.setChecked(checked);
63 return menuItem;
64 }
65
66 void CMenu::removeItem(const CMenuItem &item)
67 {
68 auto it = std::find_if(m_data->items->begin(), m_data->items->end(),
69 [=](const auto &i) { return i.m_data->index == item.m_data->index; });
70
71 XPLMRemoveMenuItem(m_data->id, it->m_data->index);
72 it = m_data->items->erase(it);
73
74 // Decrement the index of all below menu items
75 while (it != m_data->items->end())
76 {
77 it->m_data->index--;
78 ++it;
79 }
80 }
81
82 void CMenu::sep() { XPLMAppendMenuSeparator(m_data->id); }
83
84 CMenu CMenu::subMenu(const std::string &name)
85 {
86 assert(!name.empty());
87 // auto items = std::make_unique<ItemList>();
88 auto items = std::make_unique<ItemList>();
89 auto itemsVoidPtr = static_cast<void *>(&*items);
90 return { XPLMCreateMenu(name.c_str(), m_data->id, XPLMAppendMenuItem(m_data->id, name.c_str(), nullptr, false),
91 handler, itemsVoidPtr),
92 false, std::move(items) };
93 }
94
95 // cppcheck-suppress constParameter
96 void CMenu::handler(void *menuRef, void *itemRef) // cppcheck-suppress constParameterCallback
97 {
98 if (menuRef && itemRef)
99 {
100 CMenuItem *menuItem = static_cast<CMenuItem *>(itemRef);
101 menuItem->m_data->callback(menuItem->getChecked());
102 }
103 }
104
105 CMenuItem::CMenuItem(XPLMMenuID parent, int item, bool checkable, std::function<void(bool)> callback)
106 : m_data(std::make_shared<Data>(parent, item, checkable, callback))
107 {}
108
109 bool CMenuItem::getChecked() const
110 {
111 XPLMMenuCheck check = xplm_Menu_NoCheck;
112 XPLMCheckMenuItemState(m_data->parent, m_data->index, &check);
113 return check == xplm_Menu_Checked;
114 }
115
116 void CMenuItem::setChecked(bool checked)
117 {
118 XPLMCheckMenuItem(m_data->parent, m_data->index, checked ? xplm_Menu_Checked : xplm_Menu_Unchecked);
119 }
120
121 void CMenuItem::setEnabled(bool enabled) { XPLMEnableMenuItem(m_data->parent, m_data->index, enabled); }
122
123} // namespace XSwiftBus
124
CMenu()=default
Construct an uninitialized menu object.
Plugin loaded by X-Plane which publishes a DBus service.
Definition command.h:14