@@ -16,49 +16,36 @@ class MenuItem::Impl {
1616 std::string tooltip_;
1717};
1818
19- MenuItem::MenuItem () : pimpl_(new Impl(nullptr )) {
20- id = -1 ;
21- }
22-
2319MenuItem::MenuItem (void * menu_item) : pimpl_(new Impl((GtkWidget*)menu_item)) {
24- id = -1 ;
2520}
2621
2722MenuItem::~MenuItem () {
28- delete pimpl_;
2923}
3024
31- void MenuItem::SetIcon (std::string icon) {
25+ void MenuItem::SetIcon (const std::string& icon) {
3226 pimpl_->icon_ = icon;
3327 // TODO: Implement icon setting for GTK menu item
3428}
3529
36- std::string MenuItem::GetIcon () {
30+ std::string MenuItem::GetIcon () const {
3731 return pimpl_->icon_ ;
3832}
3933
40- void MenuItem::SetTitle (std::string title) {
41- pimpl_->title_ = title;
42- if (pimpl_->gtk_menu_item_ ) {
43- gtk_menu_item_set_label (GTK_MENU_ITEM (pimpl_->gtk_menu_item_ ), title.c_str ());
44- }
45- }
46-
47- std::string MenuItem::GetTitle () {
48- return pimpl_->title_ ;
49- }
50-
51- void MenuItem::SetTooltip (std::string tooltip) {
34+ void MenuItem::SetTooltip (const std::string& tooltip) {
5235 pimpl_->tooltip_ = tooltip;
5336 if (pimpl_->gtk_menu_item_ ) {
5437 gtk_widget_set_tooltip_text (pimpl_->gtk_menu_item_ , tooltip.c_str ());
5538 }
5639}
5740
58- std::string MenuItem::GetTooltip () {
41+ std::string MenuItem::GetTooltip () const {
5942 return pimpl_->tooltip_ ;
6043}
6144
45+ void * MenuItem::GetNativeItem () const {
46+ return (void *)pimpl_->gtk_menu_item_ ;
47+ }
48+
6249// Private implementation class for Menu
6350class Menu ::Impl {
6451 public:
@@ -67,54 +54,30 @@ class Menu::Impl {
6754 GtkWidget* gtk_menu_;
6855};
6956
70- Menu::Menu () : pimpl_(new Impl(gtk_menu_new())) {
71- id = -1 ;
72- }
73-
7457Menu::Menu (void * menu) : pimpl_(new Impl((GtkWidget*)menu)) {
75- id = -1 ;
7658}
7759
7860Menu::~Menu () {
7961 if (pimpl_->gtk_menu_ ) {
8062 g_object_unref (pimpl_->gtk_menu_ );
8163 }
82- delete pimpl_;
83- }
84-
85- void Menu::AddItem (MenuItem item) {
86- if (pimpl_->gtk_menu_ && item.pimpl_ ->gtk_menu_item_ ) {
87- gtk_menu_shell_append (GTK_MENU_SHELL (pimpl_->gtk_menu_ ), item.pimpl_ ->gtk_menu_item_ );
88- }
8964}
9065
91- void Menu::RemoveItem ( MenuItem item) {
92- if (pimpl_->gtk_menu_ && item. pimpl_ -> gtk_menu_item_ ) {
93- gtk_container_remove ( GTK_CONTAINER (pimpl_->gtk_menu_ ), item. pimpl_ -> gtk_menu_item_ );
66+ void Menu::AddItem (std::shared_ptr< MenuItem> item) {
67+ if (pimpl_->gtk_menu_ && item && item-> GetNativeItem () ) {
68+ gtk_menu_shell_append ( GTK_MENU_SHELL (pimpl_->gtk_menu_ ), (GtkWidget*) item-> GetNativeItem () );
9469 }
9570}
9671
97- void Menu::AddSeparator ( ) {
98- if (pimpl_->gtk_menu_ ) {
99- GtkWidget* separator = gtk_separator_menu_item_new ( );
100- gtk_menu_shell_append ( GTK_MENU_SHELL (pimpl_-> gtk_menu_ ), separator) ;
72+ bool Menu::RemoveItem (std::shared_ptr<MenuItem> item ) {
73+ if (pimpl_->gtk_menu_ && item && item-> GetNativeItem () ) {
74+ gtk_container_remove ( GTK_CONTAINER (pimpl_-> gtk_menu_ ), ( GtkWidget*)item-> GetNativeItem () );
75+ return true ;
10176 }
77+ return false ;
10278}
10379
104- MenuItem Menu::CreateItem (std::string title) {
105- GtkWidget* menu_item = gtk_menu_item_new_with_label (title.c_str ());
106- MenuItem item (menu_item);
107- item.SetTitle (title);
108- return item;
109- }
110-
111- MenuItem Menu::CreateItem (std::string title, std::string icon) {
112- MenuItem item = CreateItem (title);
113- item.SetIcon (icon);
114- return item;
115- }
116-
117- void * Menu::GetNativeMenu () {
80+ void * Menu::GetNativeMenu () const {
11881 return (void *)pimpl_->gtk_menu_ ;
11982}
12083
0 commit comments