pub fn set_focus<W: WidgetExt>(wid: &W)
Expand description
Sets the widget which has focus at the start of the application
Examples found in repository?
examples/calculator.rs (line 159)
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
fn main() {
let app = app::App::default();
let win_w = 400;
let win_h = 500;
let border = 20;
let but_row = 180;
let mut operation = Ops::None;
let mut txt = String::from("0");
let mut old_val = String::from("0");
let mut new_val: String;
let mut wind = Window::default()
.with_label("FLTK Calc")
.with_size(win_w, win_h)
.center_screen();
wind.set_color(Color::Light3);
let mut out = Output::new(border, border, win_w - 40, 140, "");
out.set_text_size(36);
out.set_value("0");
let vpack = Pack::new(border, but_row, win_w - 40, 300, "");
let mut hpack = Pack::new(0, 0, win_w - 40, 60, "");
let but_ce = MyButton::new("CE");
let but_c = MyButton::new("C");
let but_back = MyButton::new("@<-");
let but_div = MyButton::new("/");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w - 40, 60, "");
let mut but7 = MyButton::new("7");
let mut but8 = MyButton::new("8");
let mut but9 = MyButton::new("9");
let but_mul = MyButton::new("x");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w - 40, 60, "");
let mut but4 = MyButton::new("4");
let mut but5 = MyButton::new("5");
let mut but6 = MyButton::new("6");
let but_sub = MyButton::new("-");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w - 40, 60, "");
let mut but1 = MyButton::new("1");
let mut but2 = MyButton::new("2");
let mut but3 = MyButton::new("3");
let but_add = MyButton::new("+");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w - 40, 60, "");
let mut but_dot = MyButton::new(".");
let mut but0 = MyButton::new("0");
let but_eq = MyButton::new("=");
hpack.end();
hpack.set_type(PackType::Horizontal);
vpack.end();
wind.make_resizable(false);
wind.end();
wind.show_with_args(&["-scheme", "gtk+", "-nokbd"]);
app::set_focus(&*but1);
let but_vec = vec![
&mut but1, &mut but2, &mut but3, &mut but4, &mut but5, &mut but6, &mut but7, &mut but8,
&mut but9, &mut but0,
];
let but_op_vec = vec![
but_add, but_sub, but_mul, but_div, but_c, but_ce, but_back, but_eq,
];
let (s, r) = app::channel::<Message>();
for but in but_vec {
let label = but.label();
but.emit(s, Message::Number(label.parse().unwrap()));
}
for mut but in but_op_vec {
let op = match but.label().as_str() {
"+" => Ops::Add,
"-" => Ops::Sub,
"x" => Ops::Mul,
"/" => Ops::Div,
"=" => Ops::Eq,
"CE" => Ops::CE,
"C" => Ops::C,
"@<-" => Ops::Back,
_ => Ops::None,
};
but.emit(s, Message::Op(op));
}
but_dot.emit(s, Message::Dot);
while app.wait() {
if let Some(val) = r.recv() {
match val {
Message::Number(num) => {
if out.value() == "0" {
txt.clear();
}
txt.push_str(&num.to_string());
out.set_value(txt.as_str());
}
Message::Dot => {
if operation == Ops::Eq {
txt.clear();
operation = Ops::None;
out.set_value("0.");
txt.push_str("0.");
}
if !txt.contains('.') {
txt.push('.');
out.set_value(txt.as_str());
}
}
Message::Op(op) => match op {
Ops::Add | Ops::Sub | Ops::Div | Ops::Mul => {
old_val.clear();
old_val.push_str(&out.value());
operation = op;
out.set_value("0");
}
Ops::Back => {
let val = out.value();
txt.pop();
if val.len() > 1 {
out.set_value(txt.as_str());
} else {
out.set_value("0");
}
}
Ops::CE => {
txt.clear();
old_val.clear();
txt.push('0');
out.set_value(txt.as_str());
}
Ops::C => {
txt.clear();
txt.push('0');
out.set_value(txt.as_str());
}
Ops::Eq => {
new_val = out.value();
let old: f64 = old_val.parse().unwrap();
let new: f64 = new_val.parse().unwrap();
let val = match operation {
Ops::Div => old / new,
Ops::Mul => old * new,
Ops::Add => old + new,
Ops::Sub => old - new,
_ => new,
};
operation = Ops::None;
txt = String::from("0");
out.set_value(&val.to_string());
}
_ => (),
},
}
}
}
}
More examples
examples/calculator2.rs (line 177)
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
fn main() {
let app = app::App::default();
app::set_visible_focus(false);
app::background(0x42, 0x42, 0x42);
let win_w = 400;
let win_h = 500;
let but_row = 160;
let mut operation = Ops::None;
let mut txt = String::from("0");
let mut old_val = String::from("0");
let mut new_val: String;
let mut wind = Window::default()
.with_label("FLTK Calc")
.with_size(win_w, win_h)
.center_screen();
let mut out = Frame::new(0, 0, win_w, 160, "").with_align(Align::Right | Align::Inside);
out.set_color(Color::from_hex(0x1b1b1b));
out.set_frame(FrameType::FlatBox);
out.set_label_color(Color::White);
out.set_label_size(36);
out.set_label("0");
let vpack = Pack::new(0, but_row, win_w, win_h - 170, "");
let mut hpack = Pack::new(0, 0, win_w, 68, "");
let but_ce = MyButton::new("CE");
let but_c = MyButton::new("C");
let but_back = MyButton::new("@<-");
let but_div = MyButton::new("/");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w, 68, "");
let mut but7 = MyButton::new("7");
let mut but8 = MyButton::new("8");
let mut but9 = MyButton::new("9");
let but_mul = MyButton::new("x");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w, 68, "");
let mut but4 = MyButton::new("4");
let mut but5 = MyButton::new("5");
let mut but6 = MyButton::new("6");
let but_sub = MyButton::new("-");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w, 68, "");
let mut but1 = MyButton::new("1");
let mut but2 = MyButton::new("2");
let mut but3 = MyButton::new("3");
let but_add = MyButton::new("+");
hpack.end();
hpack.set_type(PackType::Horizontal);
let mut hpack = Pack::new(0, 0, win_w, 68, "");
let mut but_dot = MyButton::new(".");
let mut but0 = MyButton::new("0");
let but_eq = MyButton::new("=");
hpack.end();
hpack.set_type(PackType::Horizontal);
vpack.end();
wind.make_resizable(false);
wind.end();
wind.show();
app::set_focus(&*but1);
app::get_system_colors();
let but_vec = vec![
&mut but1, &mut but2, &mut but3, &mut but4, &mut but5, &mut but6, &mut but7, &mut but8,
&mut but9, &mut but0,
];
let but_op_vec = vec![
but_add, but_sub, but_mul, but_div, but_c, but_ce, but_back, but_eq,
];
let (s, r) = app::channel::<Message>();
for but in but_vec {
let label = but.label();
but.emit(s, Message::Number(label.parse().unwrap()));
}
for mut but in but_op_vec {
let op = match but.label().as_str() {
"+" => Ops::Add,
"-" => Ops::Sub,
"x" => Ops::Mul,
"/" => Ops::Div,
"=" => Ops::Eq,
"CE" => Ops::CE,
"C" => Ops::C,
"@<-" => Ops::Back,
_ => Ops::None,
};
but.emit(s, Message::Op(op));
}
but_dot.emit(s, Message::Dot);
while app.wait() {
if let Some(val) = r.recv() {
match val {
Message::Number(num) => {
if out.label() == "0" {
txt.clear();
}
txt.push_str(&num.to_string());
out.set_label(txt.as_str());
}
Message::Dot => {
if operation == Ops::Eq {
txt.clear();
operation = Ops::None;
out.set_label("0.");
txt.push_str("0.");
}
if !txt.contains('.') {
txt.push('.');
out.set_label(txt.as_str());
}
}
Message::Op(op) => match op {
Ops::Add | Ops::Sub | Ops::Div | Ops::Mul => {
old_val.clear();
old_val.push_str(&out.label());
operation = op;
out.set_label("0");
}
Ops::Back => {
let val = out.label();
txt.pop();
if val.len() > 1 {
out.set_label(txt.as_str());
} else {
out.set_label("0");
}
}
Ops::CE => {
txt.clear();
old_val.clear();
txt.push('0');
out.set_label(txt.as_str());
}
Ops::C => {
txt.clear();
txt.push('0');
out.set_label(txt.as_str());
}
Ops::Eq => {
new_val = out.label();
let old: f64 = old_val.parse().unwrap();
let new: f64 = new_val.parse().unwrap();
let val = match operation {
Ops::Div => old / new,
Ops::Mul => old * new,
Ops::Add => old + new,
Ops::Sub => old - new,
_ => new,
};
operation = Ops::None;
txt = String::from("0");
out.set_label(&val.to_string());
}
_ => (),
},
}
}
}
}