|
| 1 | +//! # TreeView Sample |
| 2 | +//! |
| 3 | +//! This sample demonstrates how to create a TreeView with either a ListStore or TreeStore. |
| 4 | +
|
| 5 | +extern crate glib; |
| 6 | +extern crate gtk; |
| 7 | +extern crate gdk_pixbuf; |
| 8 | + |
| 9 | +use gtk::{ |
| 10 | + BoxExt, ContainerExt, Dialog, DialogExt, Inhibit, Label, Menu, MenuBar, MenuItem, MenuItemExt, |
| 11 | + MenuShellExt, WidgetExt, WidgetSignals, Window, WindowExt, WindowPosition, WindowType |
| 12 | +}; |
| 13 | + |
| 14 | +fn main() { |
| 15 | + if gtk::init().is_err() { |
| 16 | + println!("Failed to initialize GTK."); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + let window = Window::new(WindowType::Toplevel); |
| 21 | + |
| 22 | + window.set_title("MenuBar example"); |
| 23 | + window.set_position(WindowPosition::Center); |
| 24 | + window.set_size_request(400, 400); |
| 25 | + |
| 26 | + window.connect_delete_event(|_, _| { |
| 27 | + gtk::main_quit(); |
| 28 | + Inhibit(false) |
| 29 | + }); |
| 30 | + |
| 31 | + let h_box = gtk::Box::new(gtk::Orientation::Vertical, 10); |
| 32 | + |
| 33 | + let menu = Menu::new(); |
| 34 | + let menu_bar = MenuBar::new(); |
| 35 | + let file = MenuItem::new_with_label("File"); |
| 36 | + let about = MenuItem::new_with_label("About"); |
| 37 | + let quit = MenuItem::new_with_label("Quit"); |
| 38 | + |
| 39 | + menu.append(&about); |
| 40 | + menu.append(&quit); |
| 41 | + file.set_submenu(Some(&menu)); |
| 42 | + menu_bar.append(&file); |
| 43 | + |
| 44 | + let other_menu = Menu::new(); |
| 45 | + let sub_other_menu = Menu::new(); |
| 46 | + let other = MenuItem::new_with_label("Another"); |
| 47 | + let sub_other = MenuItem::new_with_label("Sub another"); |
| 48 | + let sub_other2 = MenuItem::new_with_label("Sub another 2"); |
| 49 | + let sub_sub_other2 = MenuItem::new_with_label("Sub sub another 2"); |
| 50 | + let sub_sub_other2_2 = MenuItem::new_with_label("Sub sub another2 2"); |
| 51 | + |
| 52 | + sub_other_menu.append(&sub_sub_other2); |
| 53 | + sub_other_menu.append(&sub_sub_other2_2); |
| 54 | + sub_other2.set_submenu(Some(&sub_other_menu)); |
| 55 | + other_menu.append(&sub_other); |
| 56 | + other_menu.append(&sub_other2); |
| 57 | + other.set_submenu(Some(&other_menu)); |
| 58 | + menu_bar.append(&other); |
| 59 | + |
| 60 | + quit.connect_activate(|_| { |
| 61 | + gtk::main_quit(); |
| 62 | + }); |
| 63 | + |
| 64 | + let label = Label::new(Some("MenuBar example")); |
| 65 | + |
| 66 | + h_box.pack_start(&menu_bar, false, false, 0); |
| 67 | + h_box.pack_start(&label, true, true, 0); |
| 68 | + window.add(&h_box); |
| 69 | + window.show_all(); |
| 70 | + |
| 71 | + about.connect_activate(move |_| { |
| 72 | + let p = Dialog::new_with_buttons(Some("About"), |
| 73 | + Some(&window), |
| 74 | + gtk::DIALOG_MODAL, |
| 75 | + &[("Ok", gtk::ResponseType::Ok as i32)]); |
| 76 | + let area = p.get_content_area(); |
| 77 | + let label = Label::new(Some("MenuBar example")); |
| 78 | + area.add(&label); |
| 79 | + p.show_all(); |
| 80 | + p.connect_response(|w, _| { |
| 81 | + w.destroy(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + gtk::main(); |
| 85 | +} |
0 commit comments