From 292739664fdcf03bf4dabd2f1680b0f7c4cb17a1 Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Sat, 10 Aug 2019 17:31:56 +0200 Subject: Support copy function in Fl_Color_Chooser Users can press ctrl-c (or ctrl-x) while using the fl_color_chooser() function or the Fl_Color_Chooser widget to copy the current color selection to the clipboard. --- src/Fl_Color_Chooser.cxx | 77 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Fl_Color_Chooser.cxx b/src/Fl_Color_Chooser.cxx index bf0003ff8..283424fca 100644 --- a/src/Fl_Color_Chooser.cxx +++ b/src/Fl_Color_Chooser.cxx @@ -3,7 +3,7 @@ // // Color chooser for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2010 by Bill Spitzak and others. +// Copyright 1998-2019 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -460,6 +460,81 @@ void Fl_Color_Chooser::mode(int newMode) choice.do_callback(); } +// Small local helper function: +// Copy hex color value ('RRGGBB') of Fl_Color_Chooser to clipboard. +// Always returns 1 (event was used). + +static int copy_rgb(double r, double g, double b) { + char buf[8]; + int len; + len = sprintf(buf, "%02X%02X%02X", int(r * 255 + .5), int(g * 255 + .5), int(b * 255 + .5)); + Fl::copy(buf, len, 1); + // printf("copied '%s' to clipboard\n", buf); // Debug + return 1; +} + +/** + Handles all events received by this widget. + + This specific handle() method processes the standard 'copy' function + as seen in other input widgets. It copies the current color value to + the clipboard as a string in RGB format ('RRGGBB'). + + This format is independent of the Fl_Color_Chooser display format + setting. No other formats are supplied. + + The keyboard events handled are: + - ctrl-c + - ctrl-x + - ctrl-Insert + + All other events are processed by the parent class \c Fl_Group. + + This enables the \b user to choose a color value, press + \p ctrl-c to copy the value to the clipboard and paste it into + a color selection widget in another application window or any + other text input (e.g. a preferences dialog or an editor). + + \note Keyboard event handling by the current focus widget has + priority, hence moving the focus to one of the buttons or + selecting text in one of the input widgets effectively + disables this special method. + + \param[in] e current event + \returns 1 if event has been handled, 0 otherwise + + \see Fl_Group::handle(int) +*/ + +int Fl_Color_Chooser::handle(int e) { + + unsigned int mods = Fl::event_state() & (FL_META | FL_CTRL | FL_ALT); + unsigned int shift = Fl::event_state() & FL_SHIFT; + + switch (e) { + case FL_KEYBOARD: + case FL_SHORTCUT: + // ignore CTRL-SHIFT-C, CTRL-SHIFT-X and CTRL-SHIFT-Insert + if (shift) + return Fl_Group::handle(e); + switch (Fl::event_key()) { + case FL_Insert: + if (mods == FL_CTRL) + return copy_rgb(r_, g_, b_); + break; + case 'c': + case 'x': + if (mods == FL_COMMAND) + return copy_rgb(r_, g_, b_); + break; + default: + break; + } + default: + break; + } + return Fl_Group::handle(e); +} //////////////////////////////////////////////////////////////// -- cgit v1.2.3