swift
menus.h
Go to the documentation of this file.
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 
4 #ifndef SWIFT_SIM_XSWIFTBUS_MENUS_H
5 #define SWIFT_SIM_XSWIFTBUS_MENUS_H
6 
8 
9 #include <XPLM/XPLMMenus.h>
10 
11 #include <functional>
12 #include <list>
13 #include <memory>
14 #include <string>
15 
16 namespace XSwiftBus
17 {
18 
19  class CMenu;
20 
24  class CMenuItem
25  {
26  public:
28  CMenuItem() = default;
29 
31  bool getChecked() const;
32 
34  void setChecked(bool checked);
35 
37  void setEnabled(bool enabled);
38 
39  private:
40  friend class CMenu;
41 
42  CMenuItem(XPLMMenuID parent, int item, bool checkable, std::function<void(bool)> callback);
43 
44  void setIndex(int index) { m_data->index = index; }
45 
46  struct Data
47  {
48  Data(XPLMMenuID parent_, int index_, bool checkable_, std::function<void(bool)> callback_)
49  : parent(parent_), index(index_), checkable(checkable_), callback(callback_)
50  {}
51  XPLMMenuID parent;
52  int index;
53  bool checkable;
54  std::function<void(bool)> callback;
55  };
56  std::shared_ptr<Data> m_data;
57  };
58 
62  class CMenu
63  {
64  public:
66  CMenu() = default;
67 
69  static CMenu mainMenu();
70 
72  CMenuItem item(const std::string &name, std::function<void()> callback);
73 
75  CMenuItem checkableItem(const std::string &name, bool checked, std::function<void(bool)> callback);
76 
78  void removeItem(const CMenuItem &item);
79 
81  void sep();
82 
84  CMenu subMenu(const std::string &name);
85 
86  private:
87  // Using std::list, since it does not invalidate pointers.
88  using ItemList = std::list<CMenuItem>;
89 
90  // CMenu(XPLMMenuID id, bool isMainMenu, std::unique_ptr<ItemList> callbacks);
91  CMenu(XPLMMenuID id, bool isMainMenu, std::unique_ptr<ItemList> items);
92 
93  static void handler(void *menuRef, void *itemRef);
94 
95  struct Data
96  {
97  Data(XPLMMenuID id_, bool isMainMenu_, std::unique_ptr<ItemList> items_)
98  : id(id_), isMainMenu(isMainMenu_), items(std::move(items_))
99  {}
100  XPLMMenuID id;
101  bool isMainMenu;
102  std::unique_ptr<ItemList> items;
103  ~Data();
104  Data(const Data &) = delete;
105  Data &operator=(const Data &) = delete;
106  };
107  std::shared_ptr<Data> m_data;
108  };
109 
110 } // namespace XSwiftBus
111 
112 #endif // SWIFT_SIM_XSWIFTBUS_MENUS_H
Class-based interface to X-Plane SDK menus.
Definition: menus.h:63
CMenuItem item(const std::string &name, std::function< void()> callback)
Appends an item to the menu and returns it.
void removeItem(const CMenuItem &item)
Removes item from the menu.
static CMenu mainMenu()
Returns a menu object representing the top-level menu of all plugins.
CMenuItem checkableItem(const std::string &name, bool checked, std::function< void(bool)> callback)
Appends a checkbox item to the menu and returns it.
CMenu()=default
Construct an uninitialized menu object.
CMenu subMenu(const std::string &name)
Appends an item to the menu which opens a sub-menu, and returns it.
void sep()
Appends a separator to the menu.
Class-based interface to X-Plane SDK menu items.
Definition: menus.h:25
bool getChecked() const
Returns true if the menu item is checked.
void setChecked(bool checked)
Sets the checked status of the menu item.
void setEnabled(bool enabled)
Enables or disabled the menu item.
CMenuItem()=default
Construct an uninitialized menu item object.
Plugin loaded by X-Plane which publishes a DBus service.
Definition: command.h:14