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.
List of all members
xtd::forms::main_menu Class Reference

#include <main_menu.h>

Definition

Represents the menu structure of a form.

Namespace
xtd::forms
Library
xtd.forms
Examples
The following code example demonstrates the use of main_menu control.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::forms;
namespace examples {
class form1 : public form {
public:
form1() {
text("Main menu example");
menu(main_menu1);
list_box1.parent(*this);
list_box1.dock(dock_style::fill);
}
protected:
void on_form_closing(form_closing_event_args& e) override {
e.cancel(message_box::show(*this, "Are you sure you want exit?", "Close Form", message_box_buttons::yes_no, message_box_icon::question) == dialog_result::no);
};
private:
void on_menu_click(object& sender, const event_args& e) {
list_box1.items().push_back(ustring::format("{} clicked", as<menu_item>(sender).text()));
list_box1.selected_index(list_box1.items().size() - 1);
if (file_exit_menu_item == sender) application::exit();
}
menu_item file_open_recent_file1_menu_item {"Path/File1", {*this, &form1::on_menu_click}};
menu_item file_open_recent_file2_menu_item {"Path/File2", {*this, &form1::on_menu_click}};
menu_item file_open_recent_file3_menu_item {"Path/File3", {*this, &form1::on_menu_click}};
menu_item file_open_recent_file4_menu_item {"Path/File4", {*this, &form1::on_menu_click}};
menu_item file_new_menu_item {system_texts::new_(), {*this, &form1::on_menu_click}, menu_images::file_new(), shortcut::cmd_n};
menu_item file_separator1_menu_item {"-"};
menu_item file_open_menu_item {system_texts::open(), {*this, &form1::on_menu_click}, menu_images::file_open(), shortcut::cmd_o};
menu_item file_open_recent_menu_item {"Open recent", {file_open_recent_file1_menu_item, file_open_recent_file2_menu_item, file_open_recent_file3_menu_item, file_open_recent_file4_menu_item}};
menu_item file_close_menu_item {system_texts::close(), {*this, &form1::on_menu_click}, shortcut::cmd_w};
menu_item file_separator2_menu_item {"-"};
menu_item file_save_menu_item {system_texts::save(), {*this, &form1::on_menu_click}, menu_images::file_save(), shortcut::cmd_s};
menu_item file_save_as_menu_item {system_texts::save_as(), {*this, &form1::on_menu_click}};
menu_item file_separator3_menu_item {"-"};
menu_item file_page_setup_menu_item {"Page &Seup...", {*this, &form1::on_menu_click}};
menu_item file_print_menu_item {system_texts::print(), {*this, &form1::on_menu_click}, menu_images::file_print(), shortcut::cmd_p};
menu_item file_print_preview_menu_item {system_texts::print_preview(), {*this, &form1::on_menu_click}, menu_images::file_print_preview()};
menu_item file_separator4_menu_item {"-"};
menu_item file_exit_menu_item {system_texts::exit(), {*this, &form1::on_menu_click}, menu_images::file_exit(), shortcut::alt_f4};
menu_item edit_undo_menu_item {system_texts::undo(), {*this, &form1::on_menu_click}, menu_images::edit_undo(), shortcut::cmd_z};
menu_item edit_redo_menu_item {system_texts::redo(), {*this, &form1::on_menu_click}, menu_images::edit_redo(), shortcut::cmd_shift_z};
menu_item edit_separator1_menu_item {"-"};
menu_item edit_cut_menu_item {system_texts::cut(), {*this, &form1::on_menu_click}, menu_images::edit_cut(), shortcut::cmd_x};
menu_item edit_copy_menu_item {system_texts::copy(), {*this, &form1::on_menu_click}, menu_images::edit_copy(), shortcut::cmd_c};
menu_item edit_paste_menu_item {system_texts::paste(), {*this, &form1::on_menu_click}, menu_images::edit_paste(), shortcut::cmd_v};
menu_item edit_separator2_menu_item {"-"};
menu_item edit_select_all_menu_item {system_texts::select_all(), {*this, &form1::on_menu_click}, shortcut::cmd_a};
menu_item edit_separator3_menu_item {"-"};
menu_item edit_options_menu_item {system_texts::options(), {*this, &form1::on_menu_click}};
menu_item view_back_menu_item {system_texts::back(), {*this, &form1::on_menu_click}, menu_images::view_back()};
menu_item view_forward_menu_item {system_texts::forward(), {*this, &form1::on_menu_click}, menu_images::view_forward()};
menu_item view_separator1_menu_item {"-"};
menu_item view_show_menu_item {"Show", {*this, &form1::on_menu_click}};
menu_item view_hide_menu_item {"Hide", {*this, &form1::on_menu_click}};
menu_item options_value_a_menu_item {"Value A", {*this, &form1::on_menu_click}, menu_item_kind::check, true, shortcut::alt_1};
menu_item options_value_b_menu_item {"Value B", {*this, &form1::on_menu_click}, menu_item_kind::check, shortcut::alt_2};
menu_item options_value_c_menu_item {"Value C", {*this, &form1::on_menu_click}, menu_item_kind::check, true, shortcut::alt_3};
menu_item options_separator1_menu_item {"-"};
menu_item options_value_d_menu_item {"Value D", {*this, &form1::on_menu_click}, menu_item_kind::radio, static_cast<shortcut>(keys::alt | keys::d)};
menu_item options_value_e_menu_item {"Value E", {*this, &form1::on_menu_click}, menu_item_kind::radio, true, static_cast<shortcut>(keys::alt | keys::e)};
menu_item options_value_f_menu_item {"Value F", {*this, &form1::on_menu_click}, menu_item_kind::radio, static_cast<shortcut>(keys::alt | keys::f)};
menu_item options_separator2_menu_item {"-"};
menu_item options_value_g_menu_item {"Value G", {*this, &form1::on_menu_click}, menu_item_kind::radio, static_cast<shortcut>(keys::alt | keys::shift | keys::left)};
menu_item options_value_h_menu_item {"Value H", {*this, &form1::on_menu_click}, menu_item_kind::radio};
menu_item options_value_i_menu_item {"Value I", {*this, &form1::on_menu_click}, menu_item_kind::radio};
menu_item help_about_menu_item {system_texts::about(), {*this, &form1::on_menu_click}};
menu_item file_menu_item {system_texts::file(), {file_new_menu_item, file_separator1_menu_item, file_open_menu_item, file_open_recent_menu_item, file_close_menu_item, file_separator2_menu_item, file_save_menu_item, file_save_as_menu_item, file_separator3_menu_item, file_page_setup_menu_item, file_print_menu_item, file_print_preview_menu_item, file_separator4_menu_item, file_exit_menu_item}};
menu_item edit_menu_item {system_texts::edit(), {edit_undo_menu_item, edit_redo_menu_item, edit_separator1_menu_item, edit_cut_menu_item, edit_copy_menu_item, edit_paste_menu_item, edit_separator2_menu_item, edit_select_all_menu_item, edit_separator3_menu_item, edit_options_menu_item}};
menu_item view_menu_item {system_texts::view(), {view_back_menu_item, view_forward_menu_item, view_separator1_menu_item, view_show_menu_item, view_hide_menu_item}};
menu_item options_menu_item {system_texts::options(), {options_value_a_menu_item, options_value_b_menu_item, options_value_c_menu_item, options_separator1_menu_item, options_value_d_menu_item, options_value_e_menu_item, options_value_f_menu_item, options_separator2_menu_item, options_value_g_menu_item, options_value_h_menu_item, options_value_i_menu_item}};
menu_item help_menu_item {system_texts::help(), {help_about_menu_item}};
main_menu main_menu1 {file_menu_item, edit_menu_item, view_menu_item, options_menu_item, help_menu_item};
list_box list_box1;
};
}
int main() {
application::run(examples::form1());
}
Examples
The following code example demonstrates the use of main_menu::create_standard_items factory.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::forms;
namespace examples {
class form1 : public form {
public:
form1() {
text("Main menu create standard items example");
menu(*main_menu1);
list_box1.parent(*this);
list_box1.dock(dock_style::fill);
}
private:
void on_menu_click(object& sender, const event_args& e) {
list_box1.items().push_back(ustring::format("{} clicked", as<menu_item>(sender).text()));
list_box1.selected_index(list_box1.items().size() - 1);
// The 10th item of menu_items is the file exit menu item.
if (*menu_items[10] == sender) application::exit();
}
vector<unique_ptr<menu_item>> menu_items;
unique_ptr<main_menu> main_menu1 = main_menu::create_standard_items(menu_items, {*this, &form1::on_menu_click});
list_box list_box1;
};
}
int main() {
application::run(examples::form1());
}
Examples:
main_menu.cpp.

Constructors

 main_menu ()
 Initialize a new instance of main_menu class. More...
 
 main_menu (const std::vector< menu_item_ref > &menu_items)
 Initialize a new instance of main_menu class. More...
 

Methods

static std::unique_ptr< xtd::forms::main_menucreate_standard_items (std::vector< std::unique_ptr< menu_item >> &menu_items, const xtd::event_handler &on_click)
 A factory to create a main menu with specified on click event handler. More...
 
static std::unique_ptr< xtd::forms::main_menucreate_standard_items (std::vector< std::unique_ptr< menu_item >> &menu_items, const xtd::drawing::size &image_size, const xtd::event_handler &on_click)
 A factory to create a main menu with specified image size and on click event handler. More...
 
static std::unique_ptr< xtd::forms::main_menucreate_standard_items (std::vector< std::unique_ptr< menu_item >> &menu_items, const xtd::ustring &theme, const xtd::event_handler &on_click)
 A factory to create a main menu with specified theme and on click event handler. More...
 
static std::unique_ptr< xtd::forms::main_menucreate_standard_items (std::vector< std::unique_ptr< menu_item >> &menu_items, const xtd::ustring &theme, const xtd::drawing::size &size, const xtd::event_handler &on_click)
 A factory to create a main menu with specified theme, image size and on click event handler. More...
 
static std::unique_ptr< xtd::forms::main_menucreate_standard_items (std::vector< std::unique_ptr< menu_item >> &menu_items, const xtd::forms::theme_images &theme, const xtd::drawing::size &size, const xtd::event_handler &on_click)
 A factory to create a main menu with specified theme, image size and on click event handler. More...
 

Protected methods

intptr_t create_menu_handle () override
 Creates a new handle to the Menu. More...
 
void destroy_menu_handle (intptr_t handle) override
 Destroys the handle to the Menu. More...
 
void on_item_added (size_t pos, menu_item_ref item) override
 
void on_item_removed (size_t pos, menu_item_ref item) override
 

Additional Inherited Members

- Public Types inherited from xtd::forms::menu
using menu_item_collection = layout::arranged_element_collection< menu_item_ref >
 Represents a collection of menu_item objects. More...
 
- Static Public Attributes inherited from xtd::forms::menu
static constexpr const int find_handle = 0
 Specifies that the find_menu_item(int32_t, intptr_t) method should search for a handle. More...
 
static constexpr const int find_shortcut = 1
 Specifies that the find_menu_item(int32_t, intptr_t) method should search for a shortcut. More...
 
- Public Member Functions inherited from xtd::forms::menu
intptr_t handle () const
 Gets a value representing the window handle for the menu. More...
 
virtual bool is_parent () const
 Gets a value indicating whether this menu contains any menu items. More...
 
const menu_itemmdi_list_item () const
 Gets a value indicating the menu_item that is used to display a list of multiple document interface (MDI) child forms. More...
 
const menu_item_collectionmenu_items () const
 Gets a value indicating the collection of menu_item objects associated with the menu. More...
 
menu_item_collectionmenu_items ()
 Gets a value indicating the collection of menu_item objects associated with the menu. More...
 
menumenu_items (const menu_item_collection &value)
 Sets a value indicating the collection of menu_item objects associated with the menu. More...
 
const xtd::ustringname () const
 Gets the name of the menu. More...
 
menuname (const xtd::ustring &value)
 Sets the name of the menu. More...
 
std::any tag () const
 Gets user-defined data associated with the control. More...
 
menutag (std::any value)
 Sets user-defined data associated with the control. More...
 
bool equals (const menu &) const noexcept override
 
bool equals (const object &) const noexcept override
 Indicates whether the current object is equal to another object of the same type. More...
 
std::optional< std::reference_wrapper< context_menu > > get_context_menu () const
 Gets the context_menu that contains this menu. More...
 
std::optional< std::reference_wrapper< main_menu > > get_main_menu () const
 Gets the main_menu that contains this menu. More...
 
virtual void merge_menu (const menu &menu_src)
 Merges the MenuItem objects of one menu with the current menu. More...
 
xtd::ustring to_string () const noexcept override
 Returns a string that represents the menu control. More...
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object. More...
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type. More...
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const
 Gets the type of the current instance. More...
 
- Public Member Functions inherited from xtd::iequatable< menu >
virtual bool equals (const menu &) const noexcept=0
 Indicates whether the current object is equal to another object of the same type. More...
 
- Static Public Member Functions inherited from xtd::object
static bool equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are considered equal. More...
 
static bool reference_equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are the same instance. More...
 
- Protected Attributes inherited from xtd::forms::menu
friend menu_item
 
- Protected Member Functions inherited from xtd::forms::menu
 menu ()
 Initializes a new instance of the Menu class. More...
 
 menu (const menu_item_collection &items)
 Initializes a new instance of the Menu class. More...
 
void clone_menu (const menu &menu_src)
 Copies the menu that is passed as a parameter to the current menu. More...
 
- Protected Member Functions inherited from xtd::forms::component
 component ()=default
 Initialises a new instance of the component class. More...
 
virtual bool can_raise_events () const
 Gets a value indicating whether the component can raise an event. More...
 
bool design_mode () const
 Gets a value that indicates whether the component is currently in design mode. More...
 

Constructor & Destructor Documentation

◆ main_menu() [1/2]

xtd::forms::main_menu::main_menu ( )

Initialize a new instance of main_menu class.

◆ main_menu() [2/2]

xtd::forms::main_menu::main_menu ( const std::vector< menu_item_ref > &  menu_items)
explicit

Initialize a new instance of main_menu class.

Parameters
menu_itemsAn array of menu_item objects that will be added to the main_menu.

Member Function Documentation

◆ create_menu_handle()

intptr_t xtd::forms::main_menu::create_menu_handle ( )
overrideprotectedvirtual

Creates a new handle to the Menu.

Returns
A handle to the menu if the method succeeds; otherwise, 0.

Implements xtd::forms::menu.

◆ create_standard_items() [1/5]

static std::unique_ptr<xtd::forms::main_menu> xtd::forms::main_menu::create_standard_items ( std::vector< std::unique_ptr< menu_item >> &  menu_items,
const xtd::event_handler on_click 
)
static

A factory to create a main menu with specified on click event handler.

Parameters
menu_itemsan array xtd::forms::menu_item which will contain the created menu items.
on_clickan event handler to respond on lick event.
Returns
new main menu instance.
Remarks
The following table shows the items contained in the menu_items array :
Position menu item
0 file
1 file new
2 file open
3 file separator1
4 file save
5 file save_as
6 file separator2
7 file print
8 file print_preview
9 file separator3
10 file exit
11 edit
12 edit undo
13 edit redo
14 edit separator1
15 edit cut
16 edit copy
17 edit paste
18 edit separator2
19 edit select_all
20 tools
21 tools customize
22 tools options
23 help
24 help contents
25 help index
26 help search
27 help separator1
28 help about

◆ create_standard_items() [2/5]

static std::unique_ptr<xtd::forms::main_menu> xtd::forms::main_menu::create_standard_items ( std::vector< std::unique_ptr< menu_item >> &  menu_items,
const xtd::drawing::size image_size,
const xtd::event_handler on_click 
)
static

A factory to create a main menu with specified image size and on click event handler.

Parameters
menu_itemsan array xtd::forms::menu_item which will contain the created menu items.
image_sizeA xtd::drawing::size that represent the menu item image size.
on_clickan event handler to respond on lick event.
Returns
new main menu instance.
Remarks
The following table shows the items contained in the menu_items array :
Position menu item
0 file
1 file new
2 file open
3 file separator1
4 file save
5 file save_as
6 file separator2
7 file print
8 file print_preview
9 file separator3
10 file exit
11 edit
12 edit undo
13 edit redo
14 edit separator1
15 edit cut
16 edit copy
17 edit paste
18 edit separator2
19 edit select_all
20 tools
21 tools customize
22 tools options
23 help
24 help contents
25 help index
26 help search
27 help separator1
28 help about

◆ create_standard_items() [3/5]

static std::unique_ptr<xtd::forms::main_menu> xtd::forms::main_menu::create_standard_items ( std::vector< std::unique_ptr< menu_item >> &  menu_items,
const xtd::ustring theme,
const xtd::event_handler on_click 
)
static

A factory to create a main menu with specified theme and on click event handler.

Parameters
menu_itemsan array xtd::forms::menu_item which will contain the created menu items.
themeThe theme of menu item image.
on_clickan event handler to respond on lick event.
Returns
new main menu instance.
Remarks
The following table shows the items contained in the menu_items array :
Position menu item
0 file
1 file new
2 file open
3 file separator1
4 file save
5 file save_as
6 file separator2
7 file print
8 file print_preview
9 file separator3
10 file exit
11 edit
12 edit undo
13 edit redo
14 edit separator1
15 edit cut
16 edit copy
17 edit paste
18 edit separator2
19 edit select_all
20 tools
21 tools customize
22 tools options
23 help
24 help contents
25 help index
26 help search
27 help separator1
28 help about

◆ create_standard_items() [4/5]

static std::unique_ptr<xtd::forms::main_menu> xtd::forms::main_menu::create_standard_items ( std::vector< std::unique_ptr< menu_item >> &  menu_items,
const xtd::ustring theme,
const xtd::drawing::size size,
const xtd::event_handler on_click 
)
static

A factory to create a main menu with specified theme, image size and on click event handler.

Parameters
menu_itemsan array xtd::forms::menu_item which will contain the created menu items.
themeThe theme of menu item image.
image_sizeA xtd::drawing::size that represent the menu item image size.
on_clickan event handler to respond on lick event.
Returns
new main menu instance.
Remarks
The following table shows the items contained in the menu_items array :
Position menu item
0 file
1 file new
2 file open
3 file separator1
4 file save
5 file save_as
6 file separator2
7 file print
8 file print_preview
9 file separator3
10 file exit
11 edit
12 edit undo
13 edit redo
14 edit separator1
15 edit cut
16 edit copy
17 edit paste
18 edit separator2
19 edit select_all
20 tools
21 tools customize
22 tools options
23 help
24 help contents
25 help index
26 help search
27 help separator1
28 help about

◆ create_standard_items() [5/5]

static std::unique_ptr<xtd::forms::main_menu> xtd::forms::main_menu::create_standard_items ( std::vector< std::unique_ptr< menu_item >> &  menu_items,
const xtd::forms::theme_images theme,
const xtd::drawing::size size,
const xtd::event_handler on_click 
)
static

A factory to create a main menu with specified theme, image size and on click event handler.

Parameters
menu_itemsan array xtd::forms::menu_item which will contain the created menu items.
themeThe theme of menu item image.
image_sizeA xtd::drawing::size that represent the menu item image size.
on_clickan event handler to respond on lick event.
Returns
new main menu instance.
Remarks
The following table shows the items contained in the menu_items array :
Position menu item
0 file
1 file new
2 file open
3 file separator1
4 file save
5 file save_as
6 file separator2
7 file print
8 file print_preview
9 file separator3
10 file exit
11 edit
12 edit undo
13 edit redo
14 edit separator1
15 edit cut
16 edit copy
17 edit paste
18 edit separator2
19 edit select_all
20 tools
21 tools customize
22 tools options
23 help
24 help contents
25 help index
26 help search
27 help separator1
28 help about

◆ destroy_menu_handle()

void xtd::forms::main_menu::destroy_menu_handle ( intptr_t  handle)
overrideprotectedvirtual

Destroys the handle to the Menu.

Parameters
handleA handle to the menu.

Reimplemented from xtd::forms::menu.


The documentation for this class was generated from the following file: