xtd 0.2.0
Loading...
Searching...
No Matches
xtd::forms::main_menu Class Reference
Inheritance diagram for xtd::forms::main_menu:
xtd::forms::menu xtd::iclonable xtd::forms::component xtd::iequatable< menu > xtd::interface xtd::object xtd::interface xtd::equality_operators< type_t, equatable_t >

Definition

Represents the menu structure of a form.

Header
#include <xtd/forms/main_menu>
Namespace
xtd::forms
Library
xtd.forms
Appearance
Windows macOS Gnome
Light  
 
 
 
 
 
Dark  
 
 
 
 
 
Examples
The following code example demonstrates the use of main_menu control.
#include <xtd/drawing/texts>
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/list_box>
#include <xtd/forms/main_menu>
#include <xtd/forms/menu_images>
#include <xtd/forms/message_box>
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::forms;
namespace main_menu_example {
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(string::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 {texts::new_(), {*this, &form1::on_menu_click}, menu_images::file_new(), shortcut::ctrl_n};
menu_item file_separator1_menu_item {"-"};
menu_item file_open_menu_item {texts::open(), {*this, &form1::on_menu_click}, menu_images::file_open(), shortcut::ctrl_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 {texts::close(), {*this, &form1::on_menu_click}, shortcut::ctrl_w};
menu_item file_separator2_menu_item {"-"};
menu_item file_save_menu_item {texts::save(), {*this, &form1::on_menu_click}, menu_images::file_save(), shortcut::ctrl_s};
menu_item file_save_as_menu_item {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 {texts::print(), {*this, &form1::on_menu_click}, menu_images::file_print(), shortcut::ctrl_p};
menu_item file_print_preview_menu_item {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 {texts::exit(), {*this, &form1::on_menu_click}, menu_images::file_exit(), shortcut::alt_f4};
menu_item edit_undo_menu_item {texts::undo(), {*this, &form1::on_menu_click}, menu_images::edit_undo(), shortcut::ctrl_z};
menu_item edit_redo_menu_item {texts::redo(), {*this, &form1::on_menu_click}, menu_images::edit_redo(), shortcut::ctrl_shift_z};
menu_item edit_separator1_menu_item {"-"};
menu_item edit_cut_menu_item {texts::cut(), {*this, &form1::on_menu_click}, menu_images::edit_cut(), shortcut::ctrl_x};
menu_item edit_copy_menu_item {texts::copy(), {*this, &form1::on_menu_click}, menu_images::edit_copy(), shortcut::ctrl_c};
menu_item edit_paste_menu_item {texts::paste(), {*this, &form1::on_menu_click}, menu_images::edit_paste(), shortcut::ctrl_v};
menu_item edit_separator2_menu_item {"-"};
menu_item edit_select_all_menu_item {texts::select_all(), {*this, &form1::on_menu_click}, shortcut::ctrl_a};
menu_item edit_separator3_menu_item {"-"};
menu_item edit_options_menu_item {texts::options(), {*this, &form1::on_menu_click}};
menu_item view_back_menu_item {texts::back(), {*this, &form1::on_menu_click}, menu_images::view_back()};
menu_item view_forward_menu_item {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, as<shortcut>(keys::alt | keys::d)};
menu_item options_value_e_menu_item {"Value E", {*this, &form1::on_menu_click}, menu_item_kind::radio, true, as<shortcut>(keys::alt | keys::e)};
menu_item options_value_f_menu_item {"Value F", {*this, &form1::on_menu_click}, menu_item_kind::radio, as<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, as<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 {texts::about(), {*this, &form1::on_menu_click}};
menu_item file_menu_item {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 {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 {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 {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 {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;
};
}
auto main() -> int {
application::run(main_menu_example::form1 {});
}
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition event_args.h:18
static void run()
Begins running a standard application message loop on the current thread, without a form.
Provides data for the form_closing event.
Definition form_closing_event_args.h:22
Represents a window or dialog box that makes up an application's user interface.
Definition form.h:54
Represents a standard Windows list box.
Definition list_box.h:33
Represents the menu structure of a form.
Definition main_menu.h:41
Represents an individual item that is displayed within a main_menu or context_menu.
Definition menu_item.h:31
Represents the base functionality for all menus. Although tool_strip_drop_down and tool_strip_drop_do...
Definition menu.h:37
@ e
The E key.
@ text
The xtd::forms::status_bar_panel displays text in the standard font.
The xtd::drawing namespace provides access to GDI+ basic graphics functionality. More advanced functi...
Definition brush.h:18
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.h:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
The following code example demonstrates the use of main_menu::create_standard_items factory.
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/list_box>
#include <xtd/forms/main_menu>
using namespace xtd;
using namespace xtd::forms;
namespace main_menu_create_standard_items_example {
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(string::format("{} clicked", as<menu_item>(sender).text()));
list_box1.selected_index(list_box1.items().size() - 1);
// Index 0 corresponds to the file menu and index 9 corresponds to the file exit menu.
if (main_menu1.menu_items()[0].get().menu_items()[9].get() == sender) application::exit();
}
main_menu main_menu1 = main_menu::create_standard_items({*this, &form1::on_menu_click});
list_box list_box1;
};
}
auto main() -> int {
application::run(main_menu_create_standard_items_example::form1 {});
}
Examples
application_enable_dark_mode.cpp, application_enable_light_mode.cpp, demo.cpp, hello_world_xtd.cpp, main_menu.cpp, main_menu_create_standard_items.cpp, some_controls.cpp, some_system_controls.cpp, and themes.cpp.

Public Constructors

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

Public Static Methods

static xtd::forms::main_menu create_standard_items (const xtd::event_handler &on_click)
 A factory to create a main menu with specified on click event handler.
 
static xtd::forms::main_menu create_standard_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.
 
static xtd::forms::main_menu create_standard_items (const xtd::string &theme, const xtd::event_handler &on_click)
 A factory to create a main menu with specified theme and on click event handler.
 
static xtd::forms::main_menu create_standard_items (const xtd::string &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.
 

Protected Methods

xtd::uptr< xtd::objectclone () const override
 Creates a new object that is a copy of the current instance.
 
intptr create_menu_handle () override
 Creates a new handle to the Menu.
 
void destroy_menu_handle (intptr handle) override
 Destroys the handle to the Menu.
 
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.
 
- Static Public Attributes inherited from xtd::forms::menu
static constexpr int32 find_handle
 Specifies that the find_menu_item(int32, intptr) method should search for a handle.
 
static constexpr int32 find_shortcut
 Specifies that the find_menu_item(int32, intptr) method should search for a shortcut.
 
- Public Member Functions inherited from xtd::forms::menu
intptr handle () const noexcept
 Gets a value representing the window handle for the menu.
 
virtual bool is_parent () const noexcept
 Gets a value indicating whether this menu contains any menu items.
 
const menu_itemmdi_list_item () const noexcept
 Gets a value indicating the menu_item that is used to display a list of multiple document interface (MDI) child forms.
 
const menu_item_collectionmenu_items () const noexcept
 Gets a value indicating the collection of menu_item objects associated with the menu.
 
menu_item_collectionmenu_items () noexcept
 Gets a value indicating the collection of menu_item objects associated with the menu.
 
menumenu_items (const menu_item_collection &value)
 Sets a value indicating the collection of menu_item objects associated with the menu.
 
const xtd::stringname () const noexcept
 Gets the name of the menu.
 
menuname (const xtd::string &value)
 Sets the name of the menu.
 
std::any tag () const noexcept
 Gets user-defined data associated with the control.
 
menutag (std::any value)
 Sets user-defined data associated with the control.
 
bool equals (const menu &) const noexcept override
 
std::optional< std::reference_wrapper< context_menu > > get_context_menu () const noexcept
 Gets the context_menu that contains this menu.
 
std::optional< std::reference_wrapper< main_menu > > get_main_menu () const noexcept
 Gets the main_menu that contains this menu.
 
virtual void merge_menu (const menu &menu_src)
 Merges the MenuItem objects of one menu with the current menu.
 
xtd::string to_string () const noexcept override
 Returns a string that represents the menu control.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
- 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.
 
- Public Member Functions inherited from xtd::iclonable
- Static Public Member Functions inherited from xtd::forms::menu
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 
- 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.
 
 menu (const menu_item_collection &items)
 Initializes a new instance of the Menu class.
 
void clone_menu (const menu &menu_src)
 Copies the menu that is passed as a parameter to the current menu.
 
- Protected Member Functions inherited from xtd::forms::component
 component ()
 Initialises a new instance of the component class.
 
virtual bool can_raise_events () const noexcept
 Gets a value indicating whether the component can raise an event.
 
bool design_mode () const noexcept
 Gets a value that indicates whether the component is currently in design mode.
 

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_standard_items() [1/4]

static xtd::forms::main_menu xtd::forms::main_menu::create_standard_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 and sub menu_items array :
menu_index sub_menu_index menu_item sub_menu_item
0 file
0 0 file new
0 1 file open
0 2 file separator1
0 3 file save
0 4 file save_as
0 5 file separator2
0 6 file print
0 7 file print_preview
0 8 file separator3
0 9 file exit
1 edit
1 0 edit undo
1 1 edit redo
1 2 edit separator1
1 3 edit cut
1 4 edit copy
1 6 edit paste
1 6 edit separator2
1 7 edit select_all
2 tools
2 0 tools customize
2 1 tools options
3 help
3 0 help contents
3 1 help index
3 2 help search
3 3 help separator1
3 4 help about
Examples
The following example shows how to access to edit menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& edit_menu = main_menu.menu_items()[1].get();
static xtd::forms::main_menu create_standard_items(const xtd::event_handler &on_click)
A factory to create a main menu with specified on click event handler.
const menu_item_collection & menu_items() const noexcept
Gets a value indicating the collection of menu_item objects associated with the menu.
The following example shows how to access to search help menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& search_help_menu = main_menu.menu_items()[3].get()[2].get();

◆ create_standard_items() [2/4]

static xtd::forms::main_menu xtd::forms::main_menu::create_standard_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 and sub menu_items array :
menu_index sub_menu_index menu_item sub_menu_item
0 file
0 0 file new
0 1 file open
0 2 file separator1
0 3 file save
0 4 file save_as
0 5 file separator2
0 6 file print
0 7 file print_preview
0 8 file separator3
0 9 file exit
1 edit
1 0 edit undo
1 1 edit redo
1 2 edit separator1
1 3 edit cut
1 4 edit copy
1 6 edit paste
1 6 edit separator2
1 7 edit select_all
2 tools
2 0 tools customize
2 1 tools options
3 help
3 0 help contents
3 1 help index
3 2 help search
3 3 help separator1
3 4 help about
Examples
The following example shows how to access to edit menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& edit_menu = main_menu.menu_items()[1].get();
The following example shows how to access to search help menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& search_help_menu = main_menu.menu_items()[3].get()[2].get();

◆ create_standard_items() [3/4]

static xtd::forms::main_menu xtd::forms::main_menu::create_standard_items ( const xtd::string 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 and sub menu_items array :
menu_index sub_menu_index menu_item sub_menu_item
0 file
0 0 file new
0 1 file open
0 2 file separator1
0 3 file save
0 4 file save_as
0 5 file separator2
0 6 file print
0 7 file print_preview
0 8 file separator3
0 9 file exit
1 edit
1 0 edit undo
1 1 edit redo
1 2 edit separator1
1 3 edit cut
1 4 edit copy
1 6 edit paste
1 6 edit separator2
1 7 edit select_all
2 tools
2 0 tools customize
2 1 tools options
3 help
3 0 help contents
3 1 help index
3 2 help search
3 3 help separator1
3 4 help about
Examples
The following example shows how to access to edit menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& edit_menu = main_menu.menu_items()[1].get();
The following example shows how to access to search help menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& search_help_menu = main_menu.menu_items()[3].get()[2].get();

◆ create_standard_items() [4/4]

static xtd::forms::main_menu xtd::forms::main_menu::create_standard_items ( const xtd::string 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 and sub menu_items array :
menu_index sub_menu_index menu_item sub_menu_item
0 file
0 0 file new
0 1 file open
0 2 file separator1
0 3 file save
0 4 file save_as
0 5 file separator2
0 6 file print
0 7 file print_preview
0 8 file separator3
0 9 file exit
1 edit
1 0 edit undo
1 1 edit redo
1 2 edit separator1
1 3 edit cut
1 4 edit copy
1 6 edit paste
1 6 edit separator2
1 7 edit select_all
2 tools
2 0 tools customize
2 1 tools options
3 help
3 0 help contents
3 1 help index
3 2 help search
3 3 help separator1
3 4 help about
Examples
The following example shows how to access to edit menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& edit_menu = main_menu.menu_items()[1].get();
The following example shows how to access to search help menu item.
auto main_menu = create_standard_items(on_click_menu_items);
auto& search_help_menu = main_menu.menu_items()[3].get()[2].get();

◆ clone()

xtd::uptr< xtd::object > xtd::forms::main_menu::clone ( ) const
overrideprotectedvirtual

Creates a new object that is a copy of the current instance.

Returns
A new object that is a copy of this instance.
Notes to Implementers
All controls must be override the clone method.

Implements xtd::iclonable.

◆ create_menu_handle()

intptr 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.

◆ destroy_menu_handle()

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

Destroys the handle to the Menu.

Parameters
handleA handle to the menu.

Reimplemented from xtd::forms::menu.

◆ on_item_added()

void xtd::forms::main_menu::on_item_added ( size_t  pos,
menu_item_ref  item 
)
overrideprotectedvirtual

Reimplemented from xtd::forms::menu.

◆ on_item_removed()

void xtd::forms::main_menu::on_item_removed ( size_t  pos,
menu_item_ref  item 
)
overrideprotectedvirtual

Reimplemented from xtd::forms::menu.


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