pub fn is_event_ctrl() -> boolExpand description
Returns whether the event is a control key press
Examples found in repository?
examples/terminal.rs (line 384)
20fn main() {
21 let app = fltk::app::App::default();
22
23 // Set panic handler for main thread (will become UI thread)
24 std::panic::set_hook(Box::new({
25 |e| {
26 eprintln!("!!!!PANIC!!!!{:#?}", e);
27 error_box(e.to_string()); // Only works from the UI thread
28 std::process::exit(2);
29 }
30 }));
31
32 let mut main_win = Window::new(
33 2285,
34 180,
35 WIN_WIDTH,
36 WIN_HEIGHT,
37 "FLTK/Terminal Rust wrapper test",
38 );
39 main_win.set_type(WindowType::Double);
40 main_win.make_resizable(true);
41
42 let mut menu_bar = MenuBar::new(0, 0, WIN_WIDTH, 30, None);
43
44 let mut term = Terminal::new(0, 30, WIN_WIDTH, WIN_HEIGHT - 30, None);
45 term.set_label("term");
46 main_win.resizable(&term);
47 term.set_label_type(LabelType::None);
48
49 let idx = menu_bar.add_choice("Test&1");
50 menu_bar.at(idx).unwrap().set_callback({
51 let mut term1 = term.clone();
52 move |c| mb_test1_cb(c, &mut term1)
53 });
54 menu_bar
55 .at(idx)
56 .unwrap()
57 .set_shortcut(unsafe { std::mem::transmute(0x80031) }); // Alt-1
58
59 let idx = menu_bar.add_choice("Test&2");
60 menu_bar.at(idx).unwrap().set_callback({
61 let mut term1 = term.clone();
62 move |c| mb_test2_cb(c, &mut term1)
63 });
64 menu_bar
65 .at(idx)
66 .unwrap()
67 .set_shortcut(unsafe { std::mem::transmute(0x80032) }); // Alt-2
68
69 let idx = menu_bar.add_choice("Test&3");
70 menu_bar.at(idx).unwrap().set_callback({
71 let mut term1 = term.clone();
72 move |c| mb_test3_cb(c, &mut term1)
73 });
74 menu_bar
75 .at(idx)
76 .unwrap()
77 .set_shortcut(unsafe { std::mem::transmute(0x80033) }); // Alt-3
78
79 let idx = menu_bar.add_choice("Test&4");
80 menu_bar.at(idx).unwrap().set_callback({
81 let mut term1 = term.clone();
82 move |c| mb_test4_cb(c, &mut term1)
83 });
84 menu_bar
85 .at(idx)
86 .unwrap()
87 .set_shortcut(unsafe { std::mem::transmute(0x80034) }); // Alt-4
88
89 let idx = menu_bar.add_choice("Test&5");
90 menu_bar.at(idx).unwrap().set_callback({
91 let mut term1 = term.clone();
92 move |c| mb_test5_cb(c, &mut term1)
93 });
94 menu_bar
95 .at(idx)
96 .unwrap()
97 .set_shortcut(unsafe { std::mem::transmute(0x80035) }); // Alt-5
98
99 menu_bar.end();
100
101 main_win.end();
102 main_win.show();
103
104 // Worker thread that drives the startup tests
105 let _worker_thread: std::thread::JoinHandle<_> = std::thread::spawn({
106 let mut term = term.clone();
107 move || {
108 println!("Startup tests\n");
109 term.append("Startup tests\n\n");
110 term.append("<tmp>\n"); // This line will be overwritten later
111
112 term.cursor_up(2, false);
113 assert_eq!(term.text(false), "Startup tests\n\n"); // Ignores lines below cursor
114 assert_eq!(
115 term.text(true),
116 "Startup tests\n\n<tmp>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
117 );
118
119 // Testing ansi() and set_ansi() methods
120 assert!(term.ansi(), "Default ANSI mode should be ON at startup");
121 term.append("ANSI mode is \x1b[4mON\x1b[0m\n");
122 term.set_ansi(false);
123 assert!(!term.ansi());
124 term.append("ANSI mode is \x1b[4mOFF\x1b[0m\n");
125 // append() method is already being used/tested. Test the u8, ascii, and utf8 variants
126 term.append_u8(b"Appending u8 array\n");
127 term.append_ascii("Appending ASCII array ↑ (up-arrow is dropped)\n");
128 term.set_ansi(true); // Restore ANSI state
129
130 // Play with the horizontal scrollbar
131 assert_eq!(term.hscrollbar_style(), ScrollbarStyle::AUTO);
132 term.set_hscrollbar_style(ScrollbarStyle::ON);
133 assert_eq!(term.hscrollbar_style(), ScrollbarStyle::ON);
134
135 // Test show_unknown() as incidental part of testing append methods
136 term.set_show_unknown(true);
137 assert!(term.show_unknown());
138 term.append_ascii(
139 "Appending ASCII array with show_unknown() ↑ (up-arrow is three unknown bytes)\n",
140 );
141 term.set_show_unknown(false);
142 assert!(!term.show_unknown());
143
144 term.append_utf8("Appending UTF8 array ↑ (up-arrow is visible)\n");
145 term.append_utf8_u8(b"Appending UTF8 array as u8 \xe2\x86\x91 (up-arrow is visible)\n");
146
147 let r = term.cursor_row();
148 assert_eq!(term.cursor_col(), 0);
149 term.append(&format!("Testing cursor row/col {r}"));
150 assert_eq!(term.cursor_col(), 24);
151 assert_eq!(term.cursor_row(), r);
152
153 // Test cursor color methods
154 assert_eq!(
155 term.cursor_bg_color(),
156 Color::XtermGreen,
157 "Default cursor bg at startup"
158 );
159 term.set_cursor_bg_color(Color::Red);
160 assert_eq!(term.cursor_bg_color(), Color::Red);
161 term.set_cursor_fg_color(Color::Blue);
162 assert_eq!(term.cursor_bg_color(), Color::Red);
163 assert_eq!(term.cursor_fg_color(), Color::Blue);
164 term.set_cursor_bg_color(Color::XtermGreen); // Restore the defaults
165 term.set_cursor_fg_color(Color::from_hex(0xff_ff_f0));
166 assert_eq!(term.cursor_bg_color(), Color::XtermGreen);
167 assert_eq!(term.cursor_fg_color(), Color::from_hex(0xff_ff_f0));
168
169 // The default display_rows() will derive from the window size
170 let dr = term.display_rows();
171 let height = term.h();
172 assert_eq!(height, term.h());
173 assert!(dr > 20, "Default display_rows at startup");
174 term.resize(term.x(), term.y(), term.w(), height * 2);
175 assert_eq!(term.h(), height * 2);
176 assert_eq!(height * 2, term.h());
177 assert!(term.display_rows() > dr);
178 term.resize(term.x(), term.y(), term.w(), height);
179
180 // The default display_columns() will derive from the window size
181 let dc = term.display_columns();
182 assert!(dc > 80, "Default display_rows at startup");
183 term.set_display_columns(200);
184 assert_eq!(term.display_columns(), 200);
185 term.append("\n 1 2 3 4 5 6 7 8 9");
186 term.append("\n123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
187 term.append("[This text should be truncated by display_columns() call below.]\n"); // We shouldn't see this on screen
188 term.set_display_columns(90);
189 assert_eq!(term.display_columns(), 90);
190 term.set_display_columns(dc); // Set back to default
191 assert_eq!(term.display_columns(), dc);
192
193 assert_eq!(term.history_rows(), 100, "Default history_rows at startup");
194 term.set_history_rows(50);
195 assert_eq!(term.history_rows(), 50);
196 term.set_history_rows(100); // Set back to default
197 assert_eq!(term.history_rows(), 100);
198
199 let hu = term.history_use();
200 term.append(&format!(
201 "history_use = {hu} (it's not clear what this means)\n"
202 ));
203 // assert_eq!(term.history_use(), hu+1);
204
205 term.append(&format!(
206 "margins = b:{} l:{} r:{} t{}\n",
207 term.margin_bottom(),
208 term.margin_left(),
209 term.margin_right(),
210 term.margin_top()
211 ));
212 assert_eq!(term.margin_bottom(), 3);
213 assert_eq!(term.margin_left(), 3);
214 assert_eq!(term.margin_right(), 3);
215 assert_eq!(term.margin_top(), 3);
216
217 term.set_margin_bottom(5);
218 term.set_margin_left(10);
219 term.set_margin_right(15);
220 term.set_margin_top(20);
221 assert_eq!(term.margin_bottom(), 5);
222 assert_eq!(term.margin_left(), 10);
223 assert_eq!(term.margin_right(), 15);
224 assert_eq!(term.margin_top(), 20);
225
226 term.append("Single character: '");
227 term.print_char('X');
228 term.append("', single UTF-8 character: '");
229 term.print_char_utf8('↑');
230 term.append("'\n");
231
232 let rr = term.redraw_rate();
233 assert_eq!(rr, 0.1, "Default redraw rate at startup");
234 term.append(&format!("Redraw rate {rr}\n"));
235 term.set_redraw_rate(1.0);
236 assert_eq!(term.redraw_rate(), 1.0);
237 term.set_redraw_rate(rr);
238 assert_eq!(term.redraw_rate(), rr);
239
240 let rs = term.redraw_style();
241 term.append(&format!("Redraw style {rs:?}\n"));
242 assert_eq!(
243 rs,
244 RedrawStyle::RateLimited,
245 "Default redraw style at startup"
246 );
247 term.set_redraw_style(RedrawStyle::NoRedraw);
248 assert_eq!(term.redraw_style(), RedrawStyle::NoRedraw);
249 term.set_redraw_style(rs);
250 assert_eq!(term.redraw_style(), rs);
251
252 // Sanity checks: enum values are implicitly assigned in the C++ code so could change unexpectedly
253 assert_eq!(
254 RedrawStyle::NoRedraw.bits(),
255 0x0000,
256 "RedrawStyle enum values have been reassigned"
257 );
258 assert_eq!(
259 RedrawStyle::RateLimited.bits(),
260 0x0001,
261 "RedrawStyle enum values have been reassigned"
262 );
263 assert_eq!(
264 RedrawStyle::PerWrite.bits(),
265 0x0002,
266 "RedrawStyle enum values have been reassigned"
267 );
268
269 let sb = term.scrollbar();
270 let hsb = term.hscrollbar();
271 // Both vertical and horizontal scrollbars are at zero
272 assert_eq!(sb.value(), 0.0);
273 assert_eq!(hsb.value(), 0.0);
274 term.set_hscrollbar_style(ScrollbarStyle::AUTO);
275
276 term.append(&format!(
277 "Scrollbar actual size {}\n",
278 term.scrollbar_actual_size()
279 ));
280 assert_eq!(term.scrollbar_actual_size(), 16);
281 term.append(&format!("Scrollbar size {}\n", term.scrollbar_size()));
282 assert_eq!(
283 term.scrollbar_size(),
284 0,
285 "Default scrollbar size at startup"
286 );
287 term.set_scrollbar_size(40);
288 assert_eq!(term.scrollbar_size(), 40);
289 assert_eq!(term.scrollbar_actual_size(), 40);
290 term.append(&format!(
291 "Scrollbar actual size {}\n",
292 term.scrollbar_actual_size()
293 ));
294 term.set_scrollbar_size(0); // Restore default
295 assert_eq!(term.scrollbar_size(), 0);
296 assert_eq!(term.scrollbar_actual_size(), 16);
297
298 let sfc = term.selection_fg_color();
299 let sbc = term.selection_bg_color();
300 assert_eq!(sfc, Color::Black);
301 assert_eq!(sbc, Color::White);
302 term.append(&format!("Selection colors: {sfc} {sbc}\n"));
303 term.set_selection_fg_color(Color::Green);
304 term.set_selection_bg_color(Color::DarkBlue);
305 assert_eq!(term.selection_fg_color(), Color::Green);
306 assert_eq!(term.selection_bg_color(), Color::DarkBlue);
307 term.set_selection_fg_color(sfc);
308 term.set_selection_bg_color(sbc);
309 assert_eq!(term.selection_fg_color(), Color::Black);
310 assert_eq!(term.selection_bg_color(), Color::White);
311
312 let tfcd = term.text_fg_color_default();
313 let tbcd = term.text_bg_color_default();
314 assert_eq!(tfcd, Color::XtermWhite);
315 assert_eq!(tbcd, Color::TransparentBg);
316 term.append(&format!("Default text colors: {sfc} {sbc}\n"));
317 term.set_text_fg_color_default(Color::Green);
318 term.set_text_bg_color_default(Color::DarkBlue);
319 assert_eq!(term.text_fg_color_default(), Color::Green);
320 assert_eq!(term.text_bg_color_default(), Color::DarkBlue);
321 term.set_text_fg_color_default(tfcd);
322 term.set_text_bg_color_default(tbcd);
323 assert_eq!(term.text_fg_color_default(), Color::XtermWhite);
324 assert_eq!(term.text_bg_color_default(), Color::TransparentBg);
325
326 let tfc = term.text_fg_color();
327 let tbc = term.text_bg_color();
328 assert_eq!(tfc, Color::XtermWhite);
329 assert_eq!(tbc, Color::TransparentBg);
330 term.append(&format!("Text colors: {sfc} {sbc}\n"));
331 term.set_text_fg_color(Color::Green);
332 term.set_text_bg_color(Color::DarkBlue);
333 assert_eq!(term.text_fg_color(), Color::Green);
334 assert_eq!(term.text_bg_color(), Color::DarkBlue);
335 term.set_text_fg_color(tfc);
336 term.set_text_bg_color(tbc);
337 assert_eq!(term.text_fg_color(), Color::XtermWhite);
338 assert_eq!(term.text_bg_color(), Color::TransparentBg);
339
340 let tf = term.text_font();
341 term.append(&format!("Text font: {tf:?}\n"));
342 assert_eq!(tf, Font::Courier);
343 term.set_text_font(Font::Screen);
344 assert_eq!(term.text_font(), Font::Screen);
345 term.set_text_font(tf);
346 assert_eq!(term.text_font(), Font::Courier);
347
348 let ts = term.text_size();
349 let r = term.h_to_row(100);
350 let c = term.w_to_col(100);
351 term.append(&format!(
352 "Text size: {ts}, h_to_row(100): {r}, w_to_col(100): {c}\n"
353 ));
354 assert_eq!(ts, 14);
355 term.set_text_size(30);
356 assert_eq!(term.text_size(), 30);
357 term.append(&format!(
358 "Text size: {}, h_to_row(100): {}, w_to_col(100): {}\n",
359 term.text_size(),
360 term.h_to_row(100),
361 term.w_to_col(100)
362 ));
363 term.set_text_size(ts);
364 assert_eq!(term.text_size(), ts);
365 term.append(&format!(
366 "Text size: {}, h_to_row(100): {}, w_to_col(100): {}\n",
367 term.text_size(),
368 term.h_to_row(100),
369 term.w_to_col(100)
370 ));
371
372 // Keyboard handler
373 term.handle({
374 move |term, e| {
375 match e {
376 fltk::enums::Event::KeyDown
377 if fltk::app::event_key() == fltk::enums::Key::Escape =>
378 {
379 // false to let FLTK handle ESC. true to hide ESC
380 false
381 }
382
383 fltk::enums::Event::KeyDown
384 if fltk::app::event_length() == 1 && fltk::app::is_event_ctrl() =>
385 {
386 // We handle control keystroke
387 let k = fltk::app::event_text().unwrap();
388 term.append_utf8(&k);
389 true
390 }
391
392 fltk::enums::Event::KeyDown
393 if fltk::app::event_length() == 1 && !fltk::app::is_event_alt() =>
394 {
395 // We handle normal printable keystroke
396 let k = fltk::app::event_text().unwrap();
397 term.take_focus().unwrap();
398 term.append(&k);
399 true
400 }
401
402 // fltk docs say that keyboard handler should always claim Focus and Unfocus events
403 // We can do this, or else ignore them (return false)
404 // fltk::enums::Event::Focus | fltk::enums::Event::Unfocus => {
405 // term.redraw();
406 // true
407 // }
408 _ => false, // Let FLTK handle everything else
409 }
410 }
411 });
412
413 let attr_save = term.text_attrib();
414 term.set_text_attrib(Attrib::Inverse | Attrib::Italic);
415 term.append("\nStartup tests complete. Keyboard is live.\n");
416 assert_eq!(term.text_attrib(), Attrib::Inverse | Attrib::Italic);
417 term.set_text_attrib(attr_save);
418 assert_eq!(term.text_attrib(), attr_save);
419 term.redraw();
420 }
421 });
422
423 app.run().unwrap();
424}