xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
menu.h
Go to the documentation of this file.
1 #pragma once
5 #include <any>
6 #include <map>
7 #include <optional>
8 #include <string>
9 #include <xtd/iequatable.h>
10 #include "component.h"
12 
14 namespace xtd {
16  namespace forms {
18  class context_menu;
19  class main_menu;
20  class menu_item;
22 
29  using menu_item_ref = std::reference_wrapper<menu_item>;
30 
37  using const_menu_item_ref = std::reference_wrapper<const menu_item>;
38 
49  class forms_export_ menu : public component, public iequatable<menu> {
50  public:
52 
57 
59 
62  static constexpr const int find_handle = 0;
64  static constexpr const int find_shortcut = 1;
66 
68  ~menu();
70 
72 
77  intptr_t handle() const {return data_->handle_;}
78 
81  virtual bool is_parent() const;
82 
86  const menu_item& mdi_list_item() const;
87 
91  const menu_item_collection& menu_items() const {return data_->menu_items_;}
95  menu_item_collection& menu_items() {return data_->menu_items_;}
100  menu& menu_items(const menu_item_collection& value);
102  menu& menu_items(const std::initializer_list<menu_item_ref>& value);
103  menu& menu_items(const std::vector<menu_item_ref>& value);
105 
109  const xtd::ustring& name() const {return data_->name_;}
110 
115  menu& name(const xtd::ustring& value) {
116  data_->name_ = value;
117  return *this;
118  }
119 
122  std::any tag() const {return data_->tag_;}
126  menu& tag(std::any value) {
127  data_->tag_ = value;
128  return *this;
129  }
131 
133 
135  bool equals(const menu&) const noexcept override;
136  bool equals(const object&) const noexcept override;
137 
141  std::optional<std::reference_wrapper<context_menu>> get_context_menu() const;
142 
146  std::optional<std::reference_wrapper<main_menu>> get_main_menu() const;
147 
151  virtual void merge_menu(const menu& menu_src);
152 
156  xtd::ustring to_string() const noexcept override;
158 
160  friend std::ostream& operator<<(std::ostream& os, const xtd::forms::menu& menu) noexcept {
161  return os << menu.to_string();
162  }
164 
165  protected:
166  friend menu_item;
167 
169 
173  menu();
177  explicit menu(const menu_item_collection& items);
179 
181  explicit menu(const std::initializer_list<menu_item_ref>& items);
182  explicit menu(const std::vector<menu_item_ref>& items);
183  menu(const menu&) = delete;
185 
187 
192  void clone_menu(const menu& menu_src);
193 
196  virtual intptr_t create_menu_handle() = 0;
197 
200  virtual void destroy_menu_handle(intptr_t handle) {}
201 
202  virtual void on_item_added(size_t pos, menu_item_ref item) {}
203  virtual void on_item_removed(size_t pos, menu_item_ref item) {}
205 
207  virtual void create_menu();
208  virtual void destroy_menu();
209  void recreate_menu();
210 
211  struct data {
212  intptr_t handle_ = 0;
213  std::optional<std::reference_wrapper<menu>> context_menu_;
214  menu_item_collection menu_items_;
215  std::unique_ptr<menu_item> mdi_list_item_;
216  std::optional<std::reference_wrapper<menu>> main_menu_;
217  xtd::ustring name_;
218  std::optional<std::reference_wrapper<menu>> parent_;
219  std::any tag_;
220  };
221  std::shared_ptr<data> data_;
222  static std::map<int32_t, std::reference_wrapper<menu>> handles_;
224  };
225  }
226 }
xtd::ustring to_string() const noexcept override
Returns a string that represents the menu control.
menu & name(const xtd::ustring &value)
Sets the name of the menu.
Definition: menu.h:115
Contains xtd::iequatable interface.
Contains xtd::forms::arranged_element_collection collection.
std::reference_wrapper< const menu_item > const_menu_item_ref
Represents a menu item reference.
Definition: menu.h:37
intptr_t handle() const
Gets a value representing the window handle for the menu.
Definition: menu.h:77
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
const xtd::ustring & name() const
Gets the name of the menu.
Definition: menu.h:109
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Represents a collection of objects.
Definition: arranged_element_collection.h:28
virtual void destroy_menu_handle(intptr_t handle)
Destroy the handle to the Menu.
Definition: menu.h:200
menu & tag(std::any value)
Sets user-defined data associated with the control.
Definition: menu.h:126
Represents an individual item that is displayed within a main_menu or context_menu.
Definition: menu_item.h:27
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: iequatable.h:19
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
Represents the base functionality for all menus. Although tool_strip_drop_down and tool_strip_drop_do...
Definition: menu.h:49
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
Contains xtd::forms::component class.
menu_item_collection & menu_items()
Gets a value indicating the collection of menu_item objects associated with the menu.
Definition: menu.h:95
const menu_item_collection & menu_items() const
Gets a value indicating the collection of menu_item objects associated with the menu.
Definition: menu.h:91
std::reference_wrapper< menu_item > menu_item_ref
Represents a menu item reference.
Definition: menu.h:29
std::any tag() const
Gets user-defined data associated with the control.
Definition: menu.h:122
Provides the base implementation and enables object sharing between applications. ...
Definition: component.h:21
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: to_string.h:37