-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.rs
296 lines (290 loc) · 8.14 KB
/
menu.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// work with :
// gtk4-rs 0.9.2
// GTK 4.14.4
// it means in Cargo :
// [dependencies]
// gtk = { version = "0.9.1", package = "gtk4" , features = ["v4_14"] }
// in Win, to get no console at runtime : cargo rustc --release -- -C link-args=-mwindows
use gtk::{gdk::Display, gio, glib, prelude::*, Align::Start /*Align::Center, Align::End*/};
// do it local not to add 1 file for two lines !
const CSS: &str = "
.fenetre {border: solid 2px lightgrey;}
.quitter:hover {color: red; font-weight: bold;}
";
fn main() {
// Create a new application
let application = gtk::Application::builder()
.application_id("com.github.gtk-rs.examples.menubar")
.build();
application.connect_startup(on_startup);
application.connect_activate(on_activate);
application.run();
}
//
// définition du Menu
//
fn on_startup(app: >k::Application) {
//
// actions du Menu
//
let about = gio::ActionEntry::builder("about")
.activate(|_, _, _| println!("About was pressed"))
.build();
let quit = gio::ActionEntry::builder("quit")
.activate(|app: >k::Application, _, _| app.quit())
.build();
app.add_action_entries([about, quit]);
//
// menu : architecture
//
let menubar = {
let file_menu = {
let about_menu_item = gio::MenuItem::new(
Some("About"),
Some("app.about"), // action about
);
let quit_menu_item = gio::MenuItem::new(
Some("Quit"),
Some("app.quit"), // action quit
);
//
let file_menu = gio::Menu::new();
file_menu.append_item(&about_menu_item);
file_menu.append_item(&quit_menu_item);
file_menu
};
let menubar = gio::Menu::new();
menubar.append_submenu(Some("File"), &file_menu);
menubar
};
app.set_menubar(Some(&menubar));
}
//
// mise en forme de la page
//
fn on_activate(app: >k::Application) {
//
// importe CSS, boite noire pour moi :-(
//
let provider = gtk::CssProvider::new();
provider.load_from_string(CSS);
gtk::style_context_add_provider_for_display(
&Display::default().expect("Could not connect to a display."),
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
//
// window
//
let window = gtk::ApplicationWindow::builder()
.application(app)
.title("Exemple de Menu")
.default_width(800)
.default_height(600)
.show_menubar(true)
.build();
//
// widgets
//
let b1 = creer_bouton("Cacher menu");
let b2 = creer_bouton("Afficher menu");
let b3 = gtk::ToggleButton::builder().label("Plein écran").build();
let ll = gtk::Label::builder().vexpand(true).build(); // cree un espace
let bq = creer_bouton("Quitter");
bq.add_css_class("quitter");
//
let dw = gtk::DrawingArea::builder()
.hexpand(true)
.vexpand(true)
.valign(gtk::Align::Fill)
.halign(gtk::Align::Fill)
.build();
dw.add_css_class("fenetre");
//
let toto = "Hello \nDolly \nPhilippe \nPierre \nJoseph \nAlexandre";
let tv = gtk::TextView::builder()
.hexpand(true)
.left_margin(5)
.right_margin(5)
.build();
tv.buffer().set_text(&toto);
let sw = gtk::ScrolledWindow::builder()
.hexpand(true)
.child(&tv)
.has_frame(true)
.min_content_height(50)
.max_content_height(50)
.build();
// bouton choix couleur
let mycolor = gtk::ColorDialog::new();
mycolor.set_with_alpha(true);
mycolor.set_modal(false);
mycolor.set_title("Couleur fond fenêtre");
let c1 = gtk::ColorDialogButton::new(Some(mycolor));
c1.set_halign(gtk::Align::Center);
let couleur = gtk::gdk::RGBA::parse("rgba(255,100,100,1.0)");
c1.set_rgba(&(couleur.unwrap()));
//
let sc = gtk::Scale::with_range(gtk::Orientation::Horizontal, 0.0, 255.0, 1.0);
sc.set_draw_value(true);
sc.set_value_pos(gtk::PositionType::Bottom);
sc.set_digits(0);
//sc.set_width_request(150);
sc.set_value(128.0);
//
let sp = gtk::SpinButton::with_range(0.0, 255.0, 1.0);
sp.set_value(128.0);
//
// +bh-------------+
// Boxes |+bv1--+bv2----+|
// || b1 | dw ||
// || b2 |+sw---+||
// || c1 ||tv |||
// || .. || |||
// || ll || |||
// || bq |+-----+||
// |+-----+-------+|
// +---------------+
//
let bv1 = creer_box(gtk::Orientation::Vertical);
bv1.append(
&(gtk::Label::builder()
.label("ReCoucou")
.halign(Start)
.build()),
);
bv1.append(&(gtk::Label::with_mnemonic("Coucou")));
bv1.append(&(gtk::Label::new(Some("Encore Coucou"))));
bv1.append(&b1);
bv1.append(&b2);
bv1.append(&c1);
bv1.append(&b3);
bv1.append(&sc);
bv1.append(&sp);
bv1.append(&ll);
bv1.append(&bq);
//
let bv2 = creer_box(gtk::Orientation::Vertical);
bv2.set_hexpand(true);
bv2.set_vexpand(true);
bv2.append(&dw);
bv2.append(&sw);
//
let bh = creer_box(gtk::Orientation::Horizontal);
bh.set_margin_top(5);
bh.set_margin_bottom(5);
bh.set_margin_start(5);
bh.set_margin_end(5);
bh.append(&bv1);
bh.append(&bv2);
//
// connects
//
b1.connect_clicked(glib::clone!(
#[weak]
window,
move |_| {
window.set_show_menubar(false);
}
));
//
b2.connect_clicked(glib::clone!(
#[weak]
window,
move |_| {
window.set_show_menubar(true);
}
));
//
b3.connect_clicked(glib::clone!(
#[weak]
window,
move |b3| {
if b3.is_active() {
window.fullscreen();
window.set_show_menubar(false);
b3.set_label("Fenêtre");
} else {
window.unfullscreen();
window.set_show_menubar(true);
b3.set_label("Plein écran");
}
}
));
//
bq.connect_clicked(glib::clone!(
#[weak]
window,
move |_| {
window.close();
}
));
//
dw.set_draw_func(glib::clone!(
#[weak]
c1,
move |_drawing_area, cx, w, h| {
let couleur = (c1.rgba().red(), c1.rgba().green(), c1.rgba().blue()); // f32
affichage(cx, w, h, couleur);
}
));
//
c1.connect_rgba_notify(glib::clone!(
#[weak]
dw,
move |_| dw.queue_draw()
));
//
sc.connect_value_changed(glib::clone!(
#[weak]
b1,
#[weak]
sp,
move |sc| {
//b1.set_label( &(format!("{}",sc.value())) );
b1.set_label(&sc.value().to_string());
sp.set_value(sc.value());
}
));
//
sp.connect_value_changed(glib::clone!(
#[weak]
sc,
move |sp| {
sc.set_value(sp.value());
}
));
//
// window
//
window.set_child(Some(&bh));
window.present();
}
fn creer_bouton(mes: &str) -> gtk::Button {
gtk::Button::builder()
.label(mes)
.hexpand(false)
.halign(gtk::Align::Fill) // Fill,Start,End,Center,
.build()
}
fn creer_box(o: gtk::Orientation) -> gtk::Box {
const MARGE: i32 = 5;
gtk::Box::builder().orientation(o).spacing(MARGE).build()
}
fn affichage(cx: >k::cairo::Context, w: i32, h: i32, couleur: (f32, f32, f32)) {
let (r, v, b) = couleur;
let l: f64 = 10.0; // largeur carrés
cx.set_source_rgba(0.2, 0.2, 0.2, 1.0); // couleur background
cx.paint().unwrap(); // peint le fond du drawarea
cx.set_source_rgb(r.into(), v.into(), b.into()); // couleur pixel
cx.set_line_width(1.0);
for i in 1..10 {
cx.rectangle(
l * (i as f64),
l * (i as f64),
(w as f64) - 2.0 * l * (i as f64),
(h as f64) - 2.0 * l * (i as f64),
);
}
cx.stroke().unwrap();
}