lib_myfltk 0.1.0

File handling utility functions.
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! #lib_myfltk
//!
//! ## Utility functions for use with the FLTK.rs GUI crate.
//!
//! The functions in the modules below were written to help
//! with my projects that use the FLTK-RS GUI crate.
//! I've used them in several different projects
//! which is why I've kept them together in a separate crate.
//! Their greatest weakness is poor error handling, so keep that
//! in mind if you choose to use them.  By the way, I need help getting
//! those weaknesses corrected, so if you feel like taking that on,
//! please check out the issues tab in this crate's repository.
//!
//!
//!    * VERSION = "0.0.5";
//!    * AUTHOR = "John T. Reagan";
//!    * LICENSE = "MIT";
//!    * LICENSE_URL = "<https://opensource.org/licenses/MIT>";
//!    * COPYRIGHT = "Copyright (c) 2025, John T. Reagan";
//!    * REPOSITORY = "<https://github.com/jtreagan/lib_myfltk>";


pub mod fltkutils {
    use std::cell::RefCell;
    use std::mem::take;
    use std::rc::Rc;
    use fltk::{app, button, button::Button, group, menu, output, text};
    use fltk::app::{quit, set_font_size, App};
    use fltk::enums::{Color, Shortcut};
    use fltk::prelude::{DisplayExt, GroupExt, InputExt, MenuExt, WidgetBase, WidgetExt, WindowExt};
    use fltk::text::{TextBuffer, TextEditor};
    use fltk::window::Window;
    use lib_utils::utilities::util_longest_string_in_vec;

    /// Creates a checkbox shift menu of the items passed to the function
    /// in the `flist` vector.  Returns a vector of the items that were
    /// chosen by the user.
    pub fn fltk_chkbox_shift_menu(flist: &Vec<String>) -> Vec<String> {
        let newvec: RefCell<Vec<String>> = RefCell::new(Vec::new());
        let keepers: Rc<RefCell<Vec<String>>> = Rc::new(newvec);

        let mut win = Window::default().with_size(400, 300);
        let mut row = group::Flex::default_fill().row();
        let scroll = group::Scroll::default();
        row.fixed(&scroll, 150);
        let pack = group::Pack::default().with_size(100, 300);

        for file in flist {
            let _check = button::CheckButton::default()
                .with_label(file)
                .with_size(0, 30);
        }

        pack.end();
        scroll.end();

        let mut btn = Button::default().with_label("@>");
        row.fixed(&btn, 30);
        let mut output = output::MultilineOutput::default();

        row.end();
        win.end();
        win.show();

        let keepers_clone = Rc::clone(&keepers);
        btn.set_callback(move |_b| {
            output.set_value("");
            let mut string = String::new();
            for i in 0..pack.children() {
                let check: button::CheckButton = button::CheckButton::
                from_dyn_widget(&pack.child(i).unwrap()).unwrap();
                if check.is_checked() {
                    string.push_str(&check.label());
                    string.push('\n');
                    keepers_clone.borrow_mut().push(check.label().clone());
                }
            }
            output.set_value(&string);
        });

        while win.shown() {
            app::wait();
        }

        let retvec: Vec<String> = take(&mut keepers.borrow_mut());
        retvec
    }

    /// Creates a menu of radio buttons using the `flist` vector.
    /// Active items are highlighted by a small light.
    pub fn fltk_radio_lightbtn_menu(flist: &Vec<String>) -> String {
        let newstring: RefCell<String> = RefCell::new("".to_string());
        let keepers: Rc<RefCell<String>> = Rc::new(newstring);

        let longest = util_longest_string_in_vec(&flist);

        let mut win = Window::default().with_size(400, 300);
        let flex = group::Flex::default().with_size(250, 300);   // Do you really need this?
        let scroll = group::Scroll::default().with_size(200, longest as i32 + 10);
        let pack = group::Pack::default().with_size(200, longest as i32 + 10);  // Need this to organize the buttons.

        for file in flist {
            let _radio = button::RadioLightButton::default()
                .with_label(file)
                .with_size(0, 30);
        }

        pack.end();
        scroll.end();
        flex.end();

        let mut submit = Button::new(300, 175, 75, 30, "Submit");

        win.end();
        win.show();

        let keepers_clone = Rc::clone(&keepers);
        let mut win_clone = win.clone();

        submit.set_callback(move |_b| {
            for i in 0..pack.children() {
                let radio: button::RadioLightButton = button::RadioLightButton::from_dyn_widget(&pack.child(i).unwrap()).unwrap();
                if radio.is_toggled() {
                    *keepers_clone.borrow_mut() = radio.label().clone();
                }
            }
            win_clone.hide();
        });

        while win.shown() {
            app::wait();
        }

        let ret: String = keepers.borrow().clone();
        ret
    }

    /// Creates a simple, no-frills editor using FLTK's TextEditor struct.
    /// Returns the final contents of the editor.
    pub fn fltk_simple_editor(startertxt: &str, winlabel: &str) -> String {
        let edtr = App::default();
        let mut buf = TextBuffer::default();
        let mut win = Window::default().with_size(800, 300);
        set_font_size(20);
        win.set_color(Color::Yellow);
        win.set_label(winlabel);
        win.make_resizable(true);

        fltk_simple_editor_menubar();

        buf.set_text(startertxt);
        let mut simped = TextEditor::default()
            .with_size(770, 222)
            .center_of_parent();

        simped.set_buffer(buf.clone());   // Clone is used here to avoid an ownership error.
        simped.wrap_mode(text::WrapMode::AtBounds, 0);
        simped.set_color(Color::White);
        simped.set_text_size(22);
        simped.set_text_color(Color::Black);

        win.end();
        win.show();

        edtr.run().unwrap();

        buf.text()
    }

    /// Creates a menubar to be used with the `fltk_simple_editor()`.
    /// The new menubar only has ine entry -- `File/Quit` -- that provides
    /// a pattern that can be used to create more menu items
    pub fn fltk_simple_editor_menubar() -> menu::MenuBar {

        let mut menubar = menu::MenuBar::new(0, 0, 800, 40, "");

        let quit_idx = menubar.add(
            "File/Finished\t",
            Shortcut::None,
            menu::MenuFlag::Normal,
            |_| {
                quit();
            },
        );
        menubar.at(quit_idx).unwrap().set_label_color(Color::Red);

        menubar
    }

    /// Replaces highlighted text in a `TextEditor` with the text
    /// passe in the `rpltxt` parameter.
    pub fn fltk_replace_highlighted_text(edtr: &TextEditor, buf: &mut TextBuffer, rpltxt: &str) {
        let (x, y) = match edtr.buffer().unwrap().selection_position() {
            Some(position) => position,
            None => panic!("\nError!  Could not find a cursor position in the editor.\n"),
        };

        buf.remove(x, y);                         // Remove the selected text
        buf.insert(x, rpltxt);                    // Insert new text and
        edtr.buffer().unwrap().unselect();        // Unhighlight text
    }

    /// Creates a popup window that contains two buttons.
    ///
    /// Example:
    ///
    ///     use fltk::{app, window};
    ///     use fltk::enums::Color;
    ///     use fltk::prelude::{GroupExt, WidgetBase, WidgetExt};
    ///     use lib_myfltk::fltkutils::*;
    ///
    ///     fn main() {
    ///         let app = app::App::default();
    ///
    ///         let mut primwin = window::Window::new(1000, 100, 700, 850, "Two Button Popup Example");
    ///         primwin.set_color(Color::Yellow);
    ///         primwin.end();
    ///         primwin.show();
    ///
    ///         let bttn1click = || {
    ///             println!("\n Button 1 was clicked \n");
    ///         };
    ///
    ///         let bttn2click = || {
    ///             println!("\n Button 2 was clicked \n");
    ///         };
    ///
    ///         let mut popup = fltk_popup_2btn(&primwin, Box::new(bttn1click), "Button 1",
    ///                         Box::new(bttn2click), "Button 2");
    ///
    ///         popup.end();
    ///         popup.show();
    ///
    ///         app.run().unwrap();
    ///     }
    ///
    ///
    pub fn fltk_popup_2btn(primwin: &Window, mut closure1: Box<dyn FnMut() + 'static>, label1: &str,
                           mut closure2: Box<dyn FnMut() + 'static>, label2: &str) -> Window
    {

        // region Calculate the window position -- tied to the primary window.
        let win_center = fltk_find_center_wndw(primwin);
        let popwidth = 575;  // popwidth & popheight are set to accomodate the size of the buttons.
        let popheight = 100;

        let xxx = win_center.0 - popwidth / 2;
        let yyy = win_center.1 - popheight / 2;
        // endregion

        // region Create the popup window with buttons
        let popwin = Window::default().with_size(popwidth, popheight).with_pos(xxx, yyy);

        let mut but1 = Button::new(25, 25, 250, 40, label1);
        let mut but2 = Button::new(300, 25, 250, 40, label2);
        // endregion

        // region Do the button callbacks
        let mut winclone1 = popwin.clone();
        but1.set_callback(move |_| {
            closure1();
            winclone1.hide();
        });

        let mut winclone2 = popwin.clone();
        but2.set_callback(move |_| {
            closure2();
            winclone2.hide();
        });
        // endregion

        popwin
    }

    /// Returns the coordinates of the center of `win`.
    ///
    pub fn fltk_find_center_wndw(win: &Window) -> (i32, i32) {
        let xxx = win.x();
        let yyy = win.y();
        let www = win.w();
        let hhh = win.h();

        // Calculate the center position of primwin
        let center_x = xxx + www / 2;
        let center_y = yyy + hhh / 2;

        (center_x, center_y)
    }

}

pub mod input_fltk {

/*
// todo: Still getting "unused" warnings.  Fix it if you can figure out how.

*/  // TODO's

use fltk::app::App;
use fltk::{frame, group, input, window};
use fltk::enums::CallbackTrigger;
use fltk::prelude::{GroupExt, InputExt, WidgetExt, WindowExt};

/// Allows the user to input a vector of Strings.
///
pub fn input_strvec(app: &App, prompt: &str, horiz: i32, vert: i32) -> Vec<String> {
    let mut list = Vec::new();
    let mut i = input_i64(app, "How many items in your list?");

    while app.wait() && i > 0 {
        let newelem = input_string(app, prompt, horiz, vert);
        list.push(newelem);
        i -= 1;
    }

    list
}

/// Allows the user to input a vector of f64 integers.
///
pub fn input_f64vec(app: &App, prompt: &str) -> Vec<f64> {
    let mut list = Vec::new();
    let mut i = input_i64(app, "How many items in your list?");



    while app.wait() && i > 0 {
        let newelem = input_f64(app, prompt);
        list.push(newelem);
        i -= 1;
    }
    list
}

/// Allows the user to input a vector of characters.
///
pub fn input_charvec(app: &App, prompt: &str) -> Vec<char> {
    let mut list = Vec::new();
    let mut i = input_i64(app, "How many items in your list?");

    while app.wait() && i > 0 {
        let newelem = input_char(app, prompt);
        list.push(newelem);
        i -= 1;
    }
    list
}

/// Allows the user to input a vector of i64 integers.
///
pub fn input_i64vec(app: &App, prompt: &str) -> Vec<i64> {
    let mut list = Vec::new();
    let mut i = input_i64(app, "How many items in your list?");

    while app.wait() && i > 0 {
        let newelem = input_i64(app, prompt);
        list.push(newelem);
        i -= 1;
    }
    list
}



/// Uses FLTK's Input widget to prompt the user to enter String data.
///
/// Works best if you set the horiz and vert to values 10 pixels less than the size
///      of the main window with the flex size set to 10 pixels less than that.
/// -- For large input windows try 790 x 490 first.
/// -- For small input windows try 300 x 90 and adjust by trial and error.
pub fn input_string(app: &App, prompt: &str, horiz: i32, vert: i32) -> String {

    // region Set up the input window and input frame
    let mut win = window::Window::default()
        .with_size(horiz, vert)
        .with_label("Input Window");
    win.make_resizable(true);

    let flex = group::Flex::default()
        .with_size(200, 75)
        .column()
        .center_of_parent();

    let _prompttext = frame::Frame::default().with_label(prompt);
    // endregion

    // region Set up the input widget inside the frame.
    let mut input_widget = input::Input::default();
    input_widget.set_trigger(CallbackTrigger::EnterKey);

    // Set the input widget's callback.
    let mut win2 = win.clone();
    input_widget.set_callback(move |_| {
        win2.hide();
    });

    flex.end();
    win.end();
    win.show();
    // endregion

    // region Deal with the input
    while win.shown() {
        app.wait();
    }
    input_widget.value()
    // endregion
}

/// Uses FLTK's Input widget to prompt the user to enter f64 data.
///
pub fn input_f64(app: &App, prompt: &str) -> f64 {

    // region Set up the input window and input frame
    let mut win = window::Window::default()
        .with_size(400, 100)
        .with_label("Input Window");
    win.make_resizable(true);

    let flex = group::Flex::default()
        .with_size(200, 75)
        .column()
        .center_of_parent();

    let _prompttext = frame::Frame::default().with_label(prompt);
    // endregion

    // region Set up the input widget inside the frame.
    let mut input_widget = input::FloatInput::default();
    input_widget.set_trigger(CallbackTrigger::EnterKey);

    // Set the input widget's callback.
    let mut win2 = win.clone();
    input_widget.set_callback(move |_| {
        win2.hide();
    });

    flex.end();
    win.end();
    win.show();

    // endregion

    // region Deal with the input
    while win.shown() {
        app.wait();
    }
    input_widget.value().trim().parse::<f64>().unwrap()

    // endregion
}

/// Uses FLTK's Input widget to prompt the user to enter character data.
///
pub fn input_char(app: &App, prompt: &str) -> char {

    // region Set up the input window and input frame
    let mut win = window::Window::default()
        .with_size(400, 100)
        .with_label("Input Window");
    win.make_resizable(true);

    let flex = group::Flex::default()
        .with_size(200, 75)
        .column()
        .center_of_parent();

    let _prompttext = frame::Frame::default().with_label(prompt);
    // endregion

    // region Set up the input widget inside the frame.
    let mut input_widget = input::Input::default();
    input_widget.set_trigger(CallbackTrigger::EnterKey);

    // Set the input widget's callback.
    let mut win2 = win.clone();
    input_widget.set_callback(move |_| {
        win2.hide();
    });

    flex.end();
    win.end();
    win.show();
    // endregion

    // region Deal with the input
    while win.shown() {
        app.wait();
    }
    input_widget.value().chars().nth(0).unwrap()

    // endregion
}

/// Uses FLTK's Input widget to prompt the user to enter i64 integer data.
///
pub fn input_i64(app: &App, prompt: &str) -> i64 {

    // region Set up the input window and input frame
    let mut win = window::Window::default()
        .with_size(400, 100)
        .with_label("Input Window");
    win.make_resizable(true);

    let flex = group::Flex::default()
        .with_size(200, 75)
        .column()
        .center_of_parent();

    let _prompttext = frame::Frame::default().with_label(prompt);
    // endregion

    // region Set up the input widget inside the frame.
    let mut input_widget = input::IntInput::default();
    input_widget.set_trigger(CallbackTrigger::EnterKey);

    // Set the input widget's callback.
    let mut win2 = win.clone();
    input_widget.set_callback(move |_| {
        win2.hide();
    });

    flex.end();
    win.end();
    win.show();

    // endregion

    // region Deal with the input
    while win.shown() {
        app.wait();
    }
    input_widget.value().trim().parse::<i64>().unwrap()

    // endregion
}

/*
    pub fn input_str_large(app: &App, prompt: &str, horiz: i32, vert: i32) -> String {
        // Works best if you set the horiz and vert to values 10 pixels less than the size
        //      of the main window with the flex size set to 10 pixels less than that.

        let mut win = window::Window::default()
            .with_size(horiz, vert)
            .with_label("Input Window");
        win.make_resizable(true);

        let flex = group::Flex::default()
            .with_size(horiz-10, vert-10)
            .column()
            .center_of_parent();

        let _prompttext = frame::Frame::default().with_label(prompt);

        let mut input = input::MultilineInput::default();
        input.set_wrap(true);
        input.set_trigger(CallbackTrigger::EnterKey);

        let mut win2 = win.clone();
        input.set_callback(move |_input| {
            win2.hide();
        });

        flex.end();
        win.end();
        win.show();

        while win.shown() {
            app.wait();
        }

        input.value()
    }

 */  // input_str_large()
}