|
| 1 | +//! An example control gallery: a port of the same `ui` example. |
| 2 | +
|
| 3 | +extern crate ui; |
| 4 | + |
| 5 | +use ui::{BoxControl, Button, Checkbox, ColorButton, Combobox, DateTimePicker, Entry}; |
| 6 | +use ui::{FontButton, Group, InitOptions, Label, Menu, MenuItem, ProgressBar, RadioButtons}; |
| 7 | +use ui::{Separator, Slider, Spinbox, Tab, Window}; |
| 8 | +use ui::{Image, Area, AreaHandler, AreaKeyEvent, AreaDrawParams}; |
| 9 | + |
| 10 | +struct ImageAreaHandler { |
| 11 | + data: Vec<u32> |
| 12 | +} |
| 13 | + |
| 14 | +impl AreaHandler for ImageAreaHandler { |
| 15 | + fn draw(&mut self, area: &Area, area_draw_params: &AreaDrawParams) { |
| 16 | + let img = Image::new(100, 100); |
| 17 | + img.load_pixmap(0, 0, 100, 100, &self.data); |
| 18 | + area_draw_params.context.draw_image(0.0, 0.0, &img); |
| 19 | + } |
| 20 | + |
| 21 | + fn key_event(&mut self, _area: &Area, _area_key_event: &AreaKeyEvent) -> bool { |
| 22 | + println!("key"); |
| 23 | + true |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn run() { |
| 28 | + let menu = Menu::new("File"); |
| 29 | + menu.append_item("Open").on_clicked(Box::new(open_clicked)); |
| 30 | + menu.append_item("Save").on_clicked(Box::new(save_clicked)); |
| 31 | + |
| 32 | + let menu = Menu::new("Edit"); |
| 33 | + menu.append_check_item("Checkable Item"); |
| 34 | + menu.append_separator(); |
| 35 | + let item = menu.append_item("Disabled Item"); |
| 36 | + item.disable(); |
| 37 | + menu.append_preferences_item(); |
| 38 | + |
| 39 | + let menu = Menu::new("Help"); |
| 40 | + menu.append_item("Help"); |
| 41 | + menu.append_about_item(); |
| 42 | + |
| 43 | + let mainwin = Window::new("ui Control Gallery", 640, 480, true); |
| 44 | + mainwin.set_margined(true); |
| 45 | + mainwin.on_closing(Box::new(|_| { |
| 46 | + ui::quit(); |
| 47 | + false |
| 48 | + })); |
| 49 | + |
| 50 | + let vbox = BoxControl::new_vertical(); |
| 51 | + vbox.set_padded(true); |
| 52 | + mainwin.set_child(vbox.clone().into()); |
| 53 | + |
| 54 | + let hbox = BoxControl::new_horizontal(); |
| 55 | + hbox.set_padded(true); |
| 56 | + vbox.append(hbox.clone().into(), true); |
| 57 | + |
| 58 | + let group = Group::new("Basic Controls"); |
| 59 | + group.set_margined(true); |
| 60 | + hbox.append(group.clone().into(), false); |
| 61 | + |
| 62 | + let inner = BoxControl::new_vertical(); |
| 63 | + inner.set_padded(true); |
| 64 | + group.set_child(inner.clone().into()); |
| 65 | + |
| 66 | + inner.append(Button::new("Button").into(), false); |
| 67 | + inner.append(Checkbox::new("Checkbox").into(), false); |
| 68 | + let entry = Entry::new(); |
| 69 | + entry.set_text("Entry"); |
| 70 | + inner.append(entry.into(), false); |
| 71 | + inner.append(Label::new("Label").into(), false); |
| 72 | + inner.append(Separator::new_horizontal().into(), false); |
| 73 | + |
| 74 | + inner.append(DateTimePicker::new_date_picker().into(), false); |
| 75 | + inner.append(DateTimePicker::new_time_picker().into(), false); |
| 76 | + inner.append(DateTimePicker::new_date_time_picker().into(), false); |
| 77 | + |
| 78 | + inner.append(FontButton::new().into(), false); |
| 79 | + inner.append(ColorButton::new().into(), false); |
| 80 | + |
| 81 | + let inner2 = BoxControl::new_vertical(); |
| 82 | + inner2.set_padded(true); |
| 83 | + hbox.append(inner2.clone().into(), true); |
| 84 | + |
| 85 | + let group = Group::new("Numbers"); |
| 86 | + group.set_margined(true); |
| 87 | + inner2.append(group.clone().into(), false); |
| 88 | + |
| 89 | + let inner = BoxControl::new_vertical(); |
| 90 | + inner.set_padded(true); |
| 91 | + group.set_child(inner.clone().into()); |
| 92 | + |
| 93 | + let spinbox = Spinbox::new(0, 100); |
| 94 | + spinbox.on_changed(Box::new(|spinbox| update(spinbox.value()))); |
| 95 | + inner.append(spinbox.into(), false); |
| 96 | + |
| 97 | + let slider = Slider::new(0, 100); |
| 98 | + slider.on_changed(Box::new(|slider| update(slider.value()))); |
| 99 | + inner.append(slider.into(), false); |
| 100 | + |
| 101 | + let progress_bar = ProgressBar::new(); |
| 102 | + inner.append(progress_bar.into(), false); |
| 103 | + |
| 104 | + let area = Area::new(Box::new(ImageAreaHandler {data: vec![0xff123456;100*100]})); |
| 105 | + inner2.append(area.into(),false); |
| 106 | + |
| 107 | + mainwin.show(); |
| 108 | + ui::main(); |
| 109 | +} |
| 110 | + |
| 111 | +pub fn main() { |
| 112 | + ui::init(InitOptions).unwrap(); |
| 113 | + run(); |
| 114 | + ui::uninit(); |
| 115 | +} |
| 116 | + |
| 117 | +fn open_clicked(_: &MenuItem, mainwin: &Window) { |
| 118 | + match ui::open_file(mainwin) { |
| 119 | + Some(filename) => ui::msg_box(mainwin, "File selected", &*filename), |
| 120 | + None => ui::msg_box_error(mainwin, "No file selected", "Don't be alarmed!"), |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +fn save_clicked(_: &MenuItem, mainwin: &Window) { |
| 125 | + match ui::open_file(mainwin) { |
| 126 | + Some(filename) => { |
| 127 | + ui::msg_box(mainwin, "File selected (don't worry, it's still there)", &*filename) |
| 128 | + } |
| 129 | + None => ui::msg_box_error(mainwin, "No file selected", "Don't be alarmed!"), |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +fn update(_: i64) { |
| 134 | + // TODO(pcwalton) |
| 135 | +} |
| 136 | + |
0 commit comments