FLTK logo

[master] 9f4cea2 - Improve keyboard event test program: add copy button

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] 9f4cea2 - Improve keyboard event test program: add copy button "Albrecht Schlosser" 14:47 Mar 12  
 
commit 9f4cea25bf4b627e5b9998fb60d17eed8a5e73b4
Author:     Albrecht Schlosser <albrechts.fltk@online.de>
AuthorDate: Tue Mar 12 22:32:39 2024 +0100
Commit:     Albrecht Schlosser <albrechts.fltk@online.de>
CommitDate: Tue Mar 12 22:33:34 2024 +0100

    Improve keyboard event test program: add copy button
    
    ... to copy selected or the full text to the clipboard.

 test/handle_keys.cxx | 48 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 10 deletions(-)

diff --git test/handle_keys.cxx test/handle_keys.cxx
index 6aee5b0..a081de9 100644
--- test/handle_keys.cxx
+++ 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);
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.