1+ #include < iostream>
2+ #include < string>
3+ #include < gtk/gtk.h>
4+ #include " ../../menu.h"
5+
6+ namespace nativeapi {
7+
8+ // Private implementation class for MenuItem
9+ class MenuItem ::Impl {
10+ public:
11+ Impl (GtkWidget* menu_item) : gtk_menu_item_(menu_item), title_(" " ), icon_(" " ), tooltip_(" " ) {}
12+
13+ GtkWidget* gtk_menu_item_;
14+ std::string title_;
15+ std::string icon_;
16+ std::string tooltip_;
17+ };
18+
19+ MenuItem::MenuItem () : pimpl_(new Impl(nullptr )) {
20+ id = -1 ;
21+ }
22+
23+ MenuItem::MenuItem (void * menu_item) : pimpl_(new Impl((GtkWidget*)menu_item)) {
24+ id = -1 ;
25+ }
26+
27+ MenuItem::~MenuItem () {
28+ delete pimpl_;
29+ }
30+
31+ void MenuItem::SetIcon (std::string icon) {
32+ pimpl_->icon_ = icon;
33+ // TODO: Implement icon setting for GTK menu item
34+ }
35+
36+ std::string MenuItem::GetIcon () {
37+ return pimpl_->icon_ ;
38+ }
39+
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) {
52+ pimpl_->tooltip_ = tooltip;
53+ if (pimpl_->gtk_menu_item_ ) {
54+ gtk_widget_set_tooltip_text (pimpl_->gtk_menu_item_ , tooltip.c_str ());
55+ }
56+ }
57+
58+ std::string MenuItem::GetTooltip () {
59+ return pimpl_->tooltip_ ;
60+ }
61+
62+ // Private implementation class for Menu
63+ class Menu ::Impl {
64+ public:
65+ Impl (GtkWidget* menu) : gtk_menu_(menu) {}
66+
67+ GtkWidget* gtk_menu_;
68+ };
69+
70+ Menu::Menu () : pimpl_(new Impl(gtk_menu_new())) {
71+ id = -1 ;
72+ }
73+
74+ Menu::Menu (void * menu) : pimpl_(new Impl((GtkWidget*)menu)) {
75+ id = -1 ;
76+ }
77+
78+ Menu::~Menu () {
79+ if (pimpl_->gtk_menu_ ) {
80+ g_object_unref (pimpl_->gtk_menu_ );
81+ }
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+ }
89+ }
90+
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_ );
94+ }
95+ }
96+
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);
101+ }
102+ }
103+
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 () {
118+ return (void *)pimpl_->gtk_menu_ ;
119+ }
120+
121+ } // namespace nativeapi
0 commit comments