Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit bd2fe23

Browse files
Add menu bar example
1 parent c7ceae5 commit bd2fe23

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ name = "multithreading_context"
5656

5757
[[bin]]
5858
name = "simple_treeview"
59+
60+
[[bin]]
61+
name = "menu_bar"

resources/file.png

411 Bytes
Loading

src/menu_bar.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//! # MenuBar Sample
2+
//!
3+
//! This sample demonstrates how to use Menus/MenuBars and MenuItems
4+
5+
extern crate gtk;
6+
7+
use gtk::prelude::*;
8+
use gtk::{
9+
AboutDialog, CheckMenuItem, IconSize, Image, Label, Menu, MenuBar, MenuItem, Window,
10+
WindowPosition, WindowType
11+
};
12+
13+
fn main() {
14+
if gtk::init().is_err() {
15+
println!("Failed to initialize GTK.");
16+
return;
17+
}
18+
19+
let window = Window::new(WindowType::Toplevel);
20+
21+
window.set_title("MenuBar example");
22+
window.set_position(WindowPosition::Center);
23+
window.set_size_request(400, 400);
24+
25+
window.connect_delete_event(|_, _| {
26+
gtk::main_quit();
27+
Inhibit(false)
28+
});
29+
30+
let v_box = gtk::Box::new(gtk::Orientation::Vertical, 10);
31+
32+
let menu = Menu::new();
33+
let menu_bar = MenuBar::new();
34+
let file = MenuItem::new_with_label("File");
35+
let about = MenuItem::new_with_label("About");
36+
let quit = MenuItem::new_with_label("Quit");
37+
let file_item = MenuItem::new();
38+
let file_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
39+
let file_image = Image::new_from_file("resources/file.png");
40+
let file_label = Label::new(Some("File"));
41+
let folder_item = MenuItem::new();
42+
let folder_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
43+
let folder_image = Image::new_from_icon_name("folder-music-symbolic", IconSize::Menu.into());
44+
let folder_label = Label::new(Some("Folder"));
45+
let check_item = CheckMenuItem::new_with_label("Click me!");
46+
47+
file_box.pack_start(&file_image, false, false, 0);
48+
file_box.pack_start(&file_label, true, true, 0);
49+
file_item.add(&file_box);
50+
folder_box.pack_start(&folder_image, false, false, 0);
51+
folder_box.pack_start(&folder_label, true, true, 0);
52+
folder_item.add(&folder_box);
53+
menu.append(&file_item);
54+
menu.append(&folder_item);
55+
menu.append(&check_item);
56+
menu.append(&about);
57+
menu.append(&quit);
58+
file.set_submenu(Some(&menu));
59+
menu_bar.append(&file);
60+
61+
let other_menu = Menu::new();
62+
let sub_other_menu = Menu::new();
63+
let other = MenuItem::new_with_label("Another");
64+
let sub_other = MenuItem::new_with_label("Sub another");
65+
let sub_other2 = MenuItem::new_with_label("Sub another 2");
66+
let sub_sub_other2 = MenuItem::new_with_label("Sub sub another 2");
67+
let sub_sub_other2_2 = MenuItem::new_with_label("Sub sub another2 2");
68+
69+
sub_other_menu.append(&sub_sub_other2);
70+
sub_other_menu.append(&sub_sub_other2_2);
71+
sub_other2.set_submenu(Some(&sub_other_menu));
72+
other_menu.append(&sub_other);
73+
other_menu.append(&sub_other2);
74+
other.set_submenu(Some(&other_menu));
75+
menu_bar.append(&other);
76+
77+
quit.connect_activate(|_| {
78+
gtk::main_quit();
79+
});
80+
81+
let label = Label::new(Some("MenuBar example"));
82+
83+
v_box.pack_start(&menu_bar, false, false, 0);
84+
v_box.pack_start(&label, true, true, 0);
85+
window.add(&v_box);
86+
window.show_all();
87+
88+
about.connect_activate(move |_| {
89+
let p = AboutDialog::new();
90+
p.set_authors(&["gtk-rs developers"]);
91+
p.set_website_label(Some("gtk-rs"));
92+
p.set_website(Some("http://gtk-rs.org"));
93+
p.set_authors(&["Gtk-rs developers"]);
94+
p.set_title("About!");
95+
p.set_transient_for(Some(&window));
96+
p.run();
97+
p.destroy();
98+
});
99+
check_item.connect_toggled(|w| {
100+
w.set_label(if w.get_active() {
101+
"Checked"
102+
} else {
103+
"Unchecked"
104+
});
105+
});
106+
gtk::main();
107+
}

0 commit comments

Comments
 (0)