diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2024-03-12 22:32:39 +0100 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2024-03-12 22:33:34 +0100 |
| commit | 9f4cea25bf4b627e5b9998fb60d17eed8a5e73b4 (patch) | |
| tree | 13772577fca15d1536536160ee2121bd79c6746e /test | |
| parent | 14d7218acd0b600f3abca41f6b04e018d4357f0f (diff) | |
Improve keyboard event test program: add copy button
... to copy selected or the full text to the clipboard.
Diffstat (limited to 'test')
| -rw-r--r-- | test/handle_keys.cxx | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/test/handle_keys.cxx b/test/handle_keys.cxx index 6aee5b0cf..a081de933 100644 --- a/test/handle_keys.cxx +++ b/test/handle_keys.cxx @@ -21,8 +21,10 @@ #include <FL/Fl_Grid.H> #include <FL/Fl_Button.H> #include <FL/Fl_Check_Button.H> +#include <FL/fl_ask.H> #include <FL/names.h> #include <stdio.h> +#include <stdlib.h> // free() // Global variables to simplify the code @@ -266,11 +268,30 @@ void clear_cb(Fl_Button *b, void *) { Terminal *tty = ((app *)b->window())->tty; tty->clear_screen_home(); tty->clear_history(); - tty->printf("%s\n", headline_text); // test, helpful for copy to clipboard + tty->printf("%s\n", headline_text); // helpful if copied to the clipboard tty->redraw(); tty->take_focus(); } +// Copy button callback: copies the selected text to the clipboard +void copy_cb(Fl_Widget *b, void *) { + Terminal *tty = ((app *)b->window())->tty; + const char *what = "Full"; + const char *text; + int tlen = tty->selection_text_len(); + if (tlen > 0) { + text = tty->selection_text(); + what = "Selected"; + } else { + text = tty->text(); + } + tlen = (int)strlen(text); + Fl::copy(text, tlen, 1, Fl::clipboard_plain_text); + fl_message("%s text has been copied to the clipboard, length = %d.", what, tlen); + free((void *)text); + tty->take_focus(); +} + // Callback for all (light) buttons void toggle_cb(Fl_Widget *w, void *) { Terminal *tty = ((app *)w->window())->tty; @@ -309,40 +330,47 @@ int main(int argc, char **argv) { const int WW = 700, WH = 400; app *win = new app(0, 0, WW, WH); win->tty->box(FL_DOWN_BOX); + win->tty->show_unknown(true); win->tty->printf("Please press any key ...\n"); Fl_Grid *grid = new Fl_Grid(0, WH - 75, WW, 75); grid->layout(2, 5, 5, 5); keydown = new Fl_Check_Button(0, 0, 80, 30, "Keydown"); - grid->widget(keydown, 0, 1); + grid->widget(keydown, 0, 0); keydown->value(1); keydown->callback(toggle_cb); keydown->tooltip("Show FL_KEYDOWN aka FL_KEYBOARD events"); keyup = new Fl_Check_Button(0, 0, 80, 30, "Keyup"); - grid->widget(keyup, 0, 2); + grid->widget(keyup, 0, 1); keyup->value(0); keyup->callback(toggle_cb); keyup->tooltip("Show FL_KEYUP events"); - scaling = new Fl_Check_Button(0, 0, 80, 30, "GUI scaling"); - grid->widget(scaling, 1, 2); - scaling->value(1); - scaling->callback(toggle_scaling); - scaling->tooltip("Use GUI scaling shortcuts"); - shortcut = new Fl_Check_Button(0, 0, 80, 30, "Shortcut"); - grid->widget(shortcut, 0, 3); + grid->widget(shortcut, 0, 2); shortcut->value(0); shortcut->callback(toggle_cb); shortcut->tooltip("Show FL_SHORTCUT events"); + scaling = new Fl_Check_Button(0, 0, 80, 30, "GUI scaling"); + grid->widget(scaling, 0, 3); + scaling->value(0); + scaling->callback(toggle_scaling); + scaling->tooltip("Use GUI scaling shortcuts"); + toggle_scaling(scaling, 0); + Fl_Button *clear = new Fl_Button(0, 0, 80, 30, "Clear"); grid->widget(clear, 1, 0); clear->callback((Fl_Callback *)clear_cb); clear->tooltip("Clear the display"); + Fl_Button *copy = new Fl_Button(0, 0, 80, 30, "Copy"); + grid->widget(copy, 1, 1); + copy->callback(copy_cb); + copy->tooltip("Copy terminal contents to clipboard"); + Fl_Button *quit = new Fl_Button(WW - 70, WH - 50, 80, 30, "Quit"); grid->widget(quit, 1, 4); quit->box(FL_THIN_UP_BOX); |
