pub fn choice(txt: &str, b0: &str, b1: &str, b2: &str) -> Option<i32>Expand description
Displays a choice box with up to three choices. Closing the dialog returns None. Choosing a value returns its index from the arguments.
Examples found in repository?
examples/editor.rs (lines 116-121)
111fn quit_cb() {
112 STATE.with(|s| {
113 if s.saved {
114 app::quit();
115 } else {
116 let c = dialog::choice(
117 "Are you sure you want to exit without saving?",
118 "&Yes",
119 "&No",
120 "",
121 );
122 if c == Some(0) {
123 app::quit();
124 }
125 }
126 });
127}
128
129fn win_cb(_w: &mut window::Window) {
130 if app::event() == Event::Close {
131 quit_cb();
132 }
133}
134
135fn editor_cb(_e: &mut text::TextEditor) {
136 STATE.with(|s| s.saved = false);
137}
138
139fn handle_drag_drop(editor: &mut text::TextEditor) {
140 editor.handle({
141 let mut dnd = false;
142 let mut released = false;
143 let buf = editor.buffer().unwrap();
144 move |_, ev| match ev {
145 Event::DndEnter => {
146 dnd = true;
147 true
148 }
149 Event::DndDrag => true,
150 Event::DndRelease => {
151 released = true;
152 true
153 }
154 Event::Paste => {
155 if dnd && released {
156 let path = app::event_text().unwrap();
157 let path = path.trim();
158 let path = path.replace("file://", "");
159 let path = std::path::PathBuf::from(&path);
160 if path.exists() {
161 // we use a timeout to avoid pasting the path into the buffer
162 app::add_timeout(0.0, {
163 let mut buf = buf.clone();
164 move |_| match buf.load_file(&path) {
165 Ok(_) => (),
166 Err(e) => dialog::alert(&format!(
167 "An issue occured while loading the file: {e}"
168 )),
169 }
170 });
171 }
172 dnd = false;
173 released = false;
174 true
175 } else {
176 false
177 }
178 }
179 Event::DndLeave => {
180 dnd = false;
181 released = false;
182 true
183 }
184 _ => false,
185 }
186 });
187}
188
189fn menu_cb(m: &mut impl MenuExt) {
190 if let Ok(mpath) = m.item_pathname(None) {
191 let ed: text::TextEditor = app::widget_from_id("ed").unwrap();
192 match mpath.as_str() {
193 "&File/&New...\t" => {
194 STATE.with(|s| {
195 if !s.buf.text().is_empty() {
196 let c = dialog::choice(
197 "Are you sure you want to clear the buffer?",
198 "&Yes",
199 "&No",
200 "",
201 );
202 if c == Some(0) {
203 s.buf.set_text("");
204 s.saved = false;
205 }
206 }
207 });
208 }
209 "&File/&Open...\t" => {
210 if let Some(c) = nfc_get_file(dialog::NativeFileChooserType::BrowseFile) {
211 if let Ok(text) = std::fs::read_to_string(&c) {
212 STATE.with(move |s| {
213 s.buf.set_text(&text);
214 s.saved = false;
215 s.current_file = c.clone();
216 });
217 }
218 }
219 }
220 "&File/&Save\t" => {
221 STATE.with(|s| {
222 if !s.saved && s.current_file.exists() {
223 std::fs::write(&s.current_file, s.buf.text()).ok();
224 }
225 });
226 }
227 "&File/Save &as...\t" => {
228 if let Some(c) = nfc_get_file(dialog::NativeFileChooserType::BrowseSaveFile) {
229 STATE.with(move |s| {
230 std::fs::write(&c, s.buf.text()).ok();
231 s.saved = true;
232 s.current_file = c.clone();
233 });
234 }
235 }
236 "&File/&Quit\t" => quit_cb(),
237 "&Edit/Cu&t\t" => ed.cut(),
238 "&Edit/&Copy\t" => ed.copy(),
239 "&Edit/&Paste\t" => ed.paste(),
240 "&Help/&About\t" => dialog::message("A minimal text editor written using fltk-rs!"),
241 _ => unreachable!(),
242 }
243 }
244}More examples
examples/editor2.rs (lines 389-394)
362 pub fn launch(&mut self) {
363 while self.app.wait() {
364 use Message::*;
365 if let Some(msg) = self.r.recv() {
366 match msg {
367 Changed => {
368 if !self.modified {
369 self.modified = true;
370 self.menu
371 .menu
372 .find_item("&File/&Save\t")
373 .unwrap()
374 .activate();
375 self.menu
376 .menu
377 .find_item("&File/&Quit\t")
378 .unwrap()
379 .set_label_color(Color::Red);
380 let name = match &self.filename {
381 Some(f) => f.to_string_lossy().to_string(),
382 None => "(Untitled)".to_string(),
383 };
384 self.main_win.set_label(&format!("* {name} - RustyEd"));
385 }
386 }
387 New => {
388 if self.buf.text() != "" {
389 let clear = if let Some(x) = dialog::choice(
390 "File unsaved, Do you wish to continue?",
391 "&Yes",
392 "&No!",
393 "",
394 ) {
395 x == 0
396 } else {
397 false
398 };
399 if clear {
400 self.buf.set_text("");
401 }
402 }
403 }
404 Open => {
405 if let Some(c) = nfc_get_file(dialog::NativeFileChooserType::BrowseFile) {
406 if c.exists() {
407 match self.buf.load_file(&c) {
408 Ok(_) => self.filename = Some(c),
409 Err(e) => dialog::alert(&format!(
410 "An issue occured while loading the file: {e}"
411 )),
412 }
413 } else {
414 dialog::alert("File does not exist!")
415 }
416 }
417 }
418 Save => {
419 self.save_file().unwrap();
420 }
421 SaveAs => {
422 self.save_file_as().unwrap();
423 }
424 Print => {
425 let mut printer = printer::Printer::default();
426 if printer.begin_job(0).is_ok() {
427 let (w, h) = printer.printable_rect();
428 self.printable.resize(
429 self.printable.x(),
430 self.printable.y(),
431 w - 40,
432 h - 40,
433 );
434 // Needs cleanup
435 let line_count = self.printable.count_lines(
436 0,
437 self.printable.buffer().unwrap().length(),
438 true,
439 ) / 45;
440 for i in 0..=line_count {
441 self.printable.scroll(45 * i, 0);
442 printer.begin_page().ok();
443 printer.print_widget(&self.printable, 20, 20);
444 printer.end_page().ok();
445 }
446 printer.end_job();
447 }
448 }
449 Quit => {
450 if self.modified {
451 match dialog::choice(
452 "Would you like to save your work?",
453 "&Yes",
454 "&No",
455 "",
456 ) {
457 Some(0) => {
458 if self.save_file().unwrap() {
459 self.app.quit();
460 }
461 }
462 Some(1) => self.app.quit(),
463 Some(_) | None => (),
464 }
465 } else {
466 self.app.quit();
467 }
468 }
469 Cut => self.editor.cut(),
470 Copy => self.editor.copy(),
471 Paste => self.editor.paste(),
472 About => dialog::message(
473 "This is an example application written in Rust and using the FLTK Gui library.",
474 ),
475 }
476 }
477 }
478 }