Mercurial > tablet-emu
annotate src/gtk.rs @ 13:97e543f50f62
Split GTK UI into another module.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 01 Nov 2020 16:08:29 +0100 |
parents | |
children | adab13145994 |
rev | line source |
---|---|
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 // Tablet emulator, for people who don’t own one |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 // |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 // This program is free software: you can redistribute it and/or modify |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 // it under the terms of the GNU Affero General Public License as published by |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 // the Free Software Foundation, either version 3 of the License, or |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 // (at your option) any later version. |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 // |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 // This program is distributed in the hope that it will be useful, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 // GNU Affero General Public License for more details. |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 // |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 // You should have received a copy of the GNU Affero General Public License |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 // along with this program. If not, see <https://www.gnu.org/licenses/>. |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 use gio::prelude::*; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 use glib::clone; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 use gtk::prelude::*; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 use std::env::args; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 use std::io::ErrorKind; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 use std::sync::Arc; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 use input_linux::Key; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 use crate::{ |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 state::State, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 DEFAULT_WIDTH, DEFAULT_HEIGHT, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 fn build_main_menu(application: >k::Application) { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 let quit = gio::SimpleAction::new("quit", None); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 application.set_accels_for_action("app.quit", &["<Control>q"]); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 application.add_action(&quit); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 quit.connect_activate(clone!(@weak application => move |_, _| application.quit())); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 let about = gio::SimpleAction::new("about", None); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 application.add_action(&about); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 about.connect_activate(|_, _| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 let about = gtk::AboutDialog::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 about.set_program_name("TabletEmu"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 about.set_logo_icon_name(Some("input-tablet")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 about.set_website(Some("https://hg.linkmauve.fr/tablet-emu")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 about.set_version(Some("0.1")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 about.set_license_type(gtk::License::Agpl30); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 about.set_copyright(Some("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 about.run(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 unsafe { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 about.destroy(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 let menu = gio::Menu::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 let file = gio::Menu::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 file.append(Some("_Quit"), Some("app.quit")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 menu.append_submenu(Some("_File"), &file); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 let help = gio::Menu::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 help.append(Some("_About"), Some("app.about")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 menu.append_submenu(Some("_Help"), &help); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 application.set_menubar(Some(&menu)); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 fn build_ui(application: >k::Application) { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 build_main_menu(application); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 let state = match State::new() { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 Ok(state) => state, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 Err(err) => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 match err.kind() { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 ErrorKind::NotFound => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 eprintln!("Couldn’t find /dev/uinput: {}", err); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 eprintln!("Maybe you forgot to `modprobe uinput`?"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 ErrorKind::PermissionDenied => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 eprintln!("Couldn’t open /dev/uinput for writing: {}", err); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 eprintln!("Maybe you aren’t allowed to create input devices?"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err), |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 std::process::exit(1); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 let window = gtk::ApplicationWindow::new(application); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 window.set_title("tablet-emu"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 window.set_position(gtk::WindowPosition::Center); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 macro_rules! impl_tool { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 ($tool:tt) => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 let tool = gtk::Button::with_mnemonic($tool); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 let state_weak = Arc::downgrade(&state); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
99 tool.connect_clicked(move |b| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
100 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
101 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
102 let tool = match b.get_label().unwrap().as_str() { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
103 "_Pen" => Key::ButtonToolPen, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
104 "_Rubber" => Key::ButtonToolRubber, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
105 "_Brush" => Key::ButtonToolBrush, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
106 "P_encil" => Key::ButtonToolPencil, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
107 "_Airbrush" => Key::ButtonToolAirbrush, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
108 _ => unreachable!(), |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
109 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
110 state.select_tool(tool); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
111 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
112 tools_box.add(&tool); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
113 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
114 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
115 impl_tool!("_Pen"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
116 impl_tool!("_Rubber"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
117 impl_tool!("_Brush"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
118 impl_tool!("P_encil"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
119 impl_tool!("_Airbrush"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
120 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
121 let drawing_area = gtk::DrawingArea::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
122 drawing_area.set_size_request(DEFAULT_WIDTH, DEFAULT_HEIGHT); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
123 drawing_area.set_hexpand(true); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
124 drawing_area.set_events( |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
125 gdk::EventMask::BUTTON_PRESS_MASK |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
126 | gdk::EventMask::BUTTON_RELEASE_MASK |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
127 | gdk::EventMask::POINTER_MOTION_MASK, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 ); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
129 let state_weak = Arc::downgrade(&state); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
130 drawing_area.connect_configure_event(move |_, event| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
131 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
132 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
133 state.set_size(event.get_size()); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
134 true |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
135 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
136 let state_weak = Arc::downgrade(&state); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
137 drawing_area.connect_button_press_event(move |_, event| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
138 if event.get_button() != 1 { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
139 return Inhibit(false); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
140 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
141 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
142 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
143 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
144 let (x, y) = event.get_position(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
145 state.press(x, y).unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
146 Inhibit(false) |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
147 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
148 let state_weak = Arc::downgrade(&state); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
149 drawing_area.connect_button_release_event(move |_, event| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
150 if event.get_button() != 1 { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
151 return Inhibit(false); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
154 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
155 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
156 let (x, y) = event.get_position(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
157 state.release(x, y).unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
158 Inhibit(false) |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
159 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
160 drawing_area.connect_motion_notify_event(move |_, event| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
161 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
162 let (x, y) = event.get_position(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
163 state.motion(x, y).unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
164 Inhibit(false) |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
165 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
166 drawing_area.connect_draw(move |_, ctx| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
167 //println!("drawing {}", drawing_area); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
168 ctx.set_source_rgb(1., 0., 0.); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
169 ctx.set_operator(cairo::Operator::Screen); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
170 ctx.paint(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
171 Inhibit(false) |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
172 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
173 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
174 hbox.add(&tools_box); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
175 hbox.add(&drawing_area); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
176 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
177 window.add(&hbox); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
178 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
179 window.show_all(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
180 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
181 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
182 pub fn main() { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
183 let application = gtk::Application::new( |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
184 Some("fr.linkmauve.TabletEmu"), |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
185 gio::ApplicationFlags::empty(), |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
186 ) |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
187 .expect("Initialisation failed…"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
188 application.connect_activate(build_ui); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
189 application.run(&args().collect::<Vec<_>>()); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
190 } |