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

Add menu bar example #88

Merged
merged 1 commit into from
Apr 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ name = "multithreading_context"

[[bin]]
name = "simple_treeview"

[[bin]]
name = "menu_bar"
Binary file added resources/file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 107 additions & 0 deletions src/menu_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! # MenuBar Sample
//!
//! This sample demonstrates how to use Menus/MenuBars and MenuItems

extern crate gtk;

use gtk::prelude::*;
use gtk::{
AboutDialog, CheckMenuItem, IconSize, Image, Label, Menu, MenuBar, MenuItem, Window,
WindowPosition, WindowType
};
Copy link
Member

@gkoz gkoz Apr 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't import traits one by one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll replace by gtk::prelude::* after what we experienced recently.


fn main() {
if gtk::init().is_err() {
println!("Failed to initialize GTK.");
return;
}

let window = Window::new(WindowType::Toplevel);

window.set_title("MenuBar example");
window.set_position(WindowPosition::Center);
window.set_size_request(400, 400);

window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});

let v_box = gtk::Box::new(gtk::Orientation::Vertical, 10);

let menu = Menu::new();
let menu_bar = MenuBar::new();
let file = MenuItem::new_with_label("File");
let about = MenuItem::new_with_label("About");
let quit = MenuItem::new_with_label("Quit");
let file_item = MenuItem::new();
let file_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let file_image = Image::new_from_file("resources/file.png");
let file_label = Label::new(Some("File"));
let folder_item = MenuItem::new();
let folder_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let folder_image = Image::new_from_icon_name("folder-music-symbolic", IconSize::Menu.into());
let folder_label = Label::new(Some("Folder"));
let check_item = CheckMenuItem::new_with_label("Click me!");

file_box.pack_start(&file_image, false, false, 0);
file_box.pack_start(&file_label, true, true, 0);
file_item.add(&file_box);
folder_box.pack_start(&folder_image, false, false, 0);
folder_box.pack_start(&folder_label, true, true, 0);
folder_item.add(&folder_box);
menu.append(&file_item);
menu.append(&folder_item);
menu.append(&check_item);
menu.append(&about);
menu.append(&quit);
file.set_submenu(Some(&menu));
menu_bar.append(&file);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try to use Glade (UI metadata editor). No one is expected to build UIs programmatically these days.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyone can use Glade. I think it's a good thing to show how it works programatically. However, we could provide all examples in Glade version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a chance we're unaware of some obstacles to using Glade, especially around menus and actions. The GTK learning materials push strongly toward using Glade, doing a lot of these manipulations manually seems unidiomatic. So we need to prioritize making sure the Glade way works.


let other_menu = Menu::new();
let sub_other_menu = Menu::new();
let other = MenuItem::new_with_label("Another");
let sub_other = MenuItem::new_with_label("Sub another");
let sub_other2 = MenuItem::new_with_label("Sub another 2");
let sub_sub_other2 = MenuItem::new_with_label("Sub sub another 2");
let sub_sub_other2_2 = MenuItem::new_with_label("Sub sub another2 2");

sub_other_menu.append(&sub_sub_other2);
sub_other_menu.append(&sub_sub_other2_2);
sub_other2.set_submenu(Some(&sub_other_menu));
other_menu.append(&sub_other);
other_menu.append(&sub_other2);
other.set_submenu(Some(&other_menu));
menu_bar.append(&other);

quit.connect_activate(|_| {
gtk::main_quit();
});

let label = Label::new(Some("MenuBar example"));

v_box.pack_start(&menu_bar, false, false, 0);
v_box.pack_start(&label, true, true, 0);
window.add(&v_box);
window.show_all();

about.connect_activate(move |_| {
let p = AboutDialog::new();
p.set_authors(&["gtk-rs developers"]);
p.set_website_label(Some("gtk-rs"));
p.set_website(Some("http://gtk-rs.org"));
p.set_authors(&["Gtk-rs developers"]);
p.set_title("About!");
p.set_transient_for(Some(&window));
p.run();
p.destroy();
});
check_item.connect_toggled(|w| {
w.set_label(if w.get_active() {
"Checked"
} else {
"Unchecked"
});
});
gtk::main();
}