Commit 41324741 authored by mh's avatar mh

added some more libs and functionnality. app now records input for use

parent de9a4246
......@@ -210,7 +210,9 @@ dependencies = [
name = "gtk-rs-testing"
version = "0.1.0"
dependencies = [
"gdk 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gio 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"gtk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
......
......@@ -7,6 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gdk = "^0"
glib = "^0"
[dependencies.gtk]
version = "0.7.0"
features = ["v3_16"]
......
......@@ -33,7 +33,6 @@
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="_box">
......@@ -100,7 +99,7 @@
<!-- Button Box -->
<child>
<object class="GtkSeparator" id="separator">
<object class="GtkSeparator" id="separator1">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
......@@ -127,7 +126,7 @@
<!-- User Input -->
<child>
<object class="GtkEntry" id="entry">
<object class="GtkEntry" id="user_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="placeholder_text" translatable="yes">Type your text here</property>
......@@ -139,6 +138,45 @@
</packing>
</child>
<child>
<object class="GtkSeparator" id="separator2">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">6</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">Menu</property>
<property name="visible">False</property>
<property name="can_focus">False</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="placeholder_text" translatable="yes">An entry with a placeholder !</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">8</property>
</packing>
</child>
</object>
<!-- Box -->
</child>
......
......@@ -2,26 +2,100 @@
// Sandbox Environment
// mh8 - November 2019
#![cfg_attr(not(feature = "gtk_3_10"), allow(unused_variables, unused_mut))]
extern crate gdk;
extern crate gio;
extern crate glib;
extern crate gtk;
use gio::prelude::*;
use gtk::prelude::*;
use gtk::{ApplicationWindow, Builder, Button, MessageDialog};
use gtk::{
ApplicationWindow, Builder, Button, Dialog, Entry, Label, MessageDialog, PackType, ResponseType,
};
use std::env::args;
// make moving clones into closures more convenient
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
// upgrade weak reference or return
#[macro_export]
macro_rules! upgrade_weak {
($x:ident, $r:expr) => {{
match $x.upgrade() {
Some(o) => o,
None => return $r,
}
}};
($x:ident) => {
upgrade_weak!($x, ())
};
}
fn build_ui(application: &gtk::Application) {
let glade_src = include_str!("gtk-test.glade");
let builder = Builder::new_from_string(glade_src);
/*UNUSED CODE : attempt to integrate non-glade elements on top of glade failed.
let label = Label::new(Some("The label at start"));
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
vbox.add(&label);
vbox.set_child_expand(&label, false);
vbox.set_child_fill(&label, true);
vbox.set_child_padding(&label, 10);
vbox.set_child_pack_type(&label, PackType::End);
*/
let window: ApplicationWindow = builder
.get_object("window1")
.expect("Could not build window 1");
window.set_application(Some(application));
//window.add(&vbox); non-glade element
let window_weak = window.downgrade();
let button: Button = builder.get_object("button").expect("Couldn't get button");
let entry: Entry = builder.get_object("entry").expect("Couldn't get entry");
let entry_weak = entry.downgrade();
button.connect_clicked(clone!(window_weak, entry_weak => move |_| {
let window = upgrade_weak!(window_weak);
let entry = upgrade_weak!(entry_weak);
let dialog = Dialog::new_with_buttons(Some("Menu sub-Window"),
Some(&window),
gtk::DialogFlags::MODAL,
&[("No", ResponseType::No),
("Yes", ResponseType::Yes),
("Other", ResponseType::Other(0))]);
let ret = dialog.run();
dialog.destroy();
entry.set_text(&format!("Clicked {}", ret));
}));
let button_ok: Button = builder
.get_object("button1")
.expect("Could not build button ok");
......@@ -44,12 +118,33 @@ fn build_ui(application: &gtk::Application) {
dialog_cancel.hide();
});
//Display Key-pressed events in terminal
//This will not work if your focus is in a sub-window
window.connect_key_press_event(clone!(entry_weak => move |_, key| {
let entry = upgrade_weak!(entry_weak, Inhibit(false));
let keyval = key.get_keyval();
let keystate = key.get_state();
println!("key pressed: {} / {:?}", keyval, keystate);
println!("text: {}", entry.get_text().expect("Couldn't get text from entry"));
if keystate.intersects(gdk::ModifierType::CONTROL_MASK) {
println!("You pressed Ctrl!");
}
Inhibit(false)
}));
window.show_all();
}
fn main() {
let application = gtk::Application::new(Some("fr.mh8.gtk-rs-testing"), Default::default())
.expect("Initialization failed...");
let application = gtk::Application::new(
Some("fr.mh8.gtk-rs-testing"),
gio::ApplicationFlags::empty(),
)
.expect("Initialization failed...");
application.connect_activate(|app| {
build_ui(app);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment