1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2020 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #ifndef WT_WCOLORPICKER_H_
8 #define WT_WCOLORPICKER_H_
9 
10 #include <Wt/WColor.h>
11 #include <Wt/WDllDefs.h>
12 #include <Wt/WFormWidget.h>
13 #include <Wt/WObject.h>
14 #include <Wt/WSignal.h>
15 #include <Wt/WWebWidget.h>
16 
17 namespace Wt {
18 
19 /*! \class WColorPicker Wt/WColorPicker.h Wt/WColorPicker.h
20  *  \brief A widget that provides a browser-native color picker.
21  *
22  * To act upon color value changes, connect a slot to the changed()
23  * signal. This signal is emitted when the user changes the selected color,
24  * and subsequently closes the color picker.
25  *
26  * To act upon any color change, connect a slot to the colorInput() signal.
27  * Note that this signal may fire very quickly depending on how the browser's
28  * color picker works.
29  *
30  * At all times, the currently selected color may be accessed with the value() method.
31  *
32  * The widget corresponds to the HTML <tt>&lt;input type="color"&gt;</tt> tag.
33  * Note that this element does not support CSS color names.  When manipulating
34  * this widget with \link WColor WColor\endlink values, ensure they have
35  * valid RGB values or the color picker will reset to #000000.
36  *
37  * %WColorPicker is an \link WWidget::setInline(bool) inline \endlink widget.
38  *
39  * \sa WColor
40  */
41 class WT_API WColorPicker : public WFormWidget
42 {
43 public:
44   /*! \brief Creates a color picker with the default color of black (#000000).
45    */
46   WColorPicker();
47 
48   /*! \brief Creates a color picker with the given color value.
49    * Ensure the color has valid RGB values, or the color will be reset to #000000.
50    *
51    * \sa WColor::WColor(const WString&)
52    */
53   WColorPicker(const WColor& color);
54 
55   /*! \brief Returns the current value of the color picker
56    * as a \link WColor WColor\endlink object.
57    *
58    * \sa setColor(const WColor&)
59    */
60   WColor color() const;
61 
62   /*! \brief Sets the selected color.
63    *
64    * The default value is #000000 (black).
65    *
66    * Ensure the color has valid RGB values, or the color will be reset to #000000.
67    *
68    * \sa color()
69    * \sa WColor::WColor(const WString&)
70    */
71   void setColor(const WColor& value);
72 
73   /*! \brief Event signal emitted when the selected color is changed.
74    *
75    * This signal is emitted whenever the selected color has
76    * changed. Unlike the changed() signal, this signal is fired on
77    * every change, not only when the color picker is closed.
78    *
79    * In particular, on browsers with a draggable color picker (i.e. most common browsers),
80    * this signal fires every time the position changes.  Use with caution.
81    *
82    * \sa changed()
83    */
84   EventSignal<>& colorInput();
85 
86   /*! \brief Returns the current value of the color picker as a string.
87    *
88    * This is implemented as
89    * \code
90    * return color().cssText();
91    * \endcode
92    */
93   virtual WT_USTRING valueText() const override;
94 
95   /*! \brief Sets the current value of the color picker as a string.
96    * The string must be in a format from which \link WColor WColor\endlink
97    * can determine RGB values (i.e. not a CSS color name),
98    * or the value will be set to #000000.
99    *
100    * This is implemented as
101    * \code
102    * setColor(WColor(value));
103    * \endcode
104    *
105    * \sa WColor::WColor(const WString&)
106    */
107   virtual void setValueText(const WT_USTRING& value) override;
108 
109 private:
110   static const char *INPUT_SIGNAL;
111   WColor color_;
112   bool colorChanged_;
113 
114 protected:
115   virtual void updateDom(DomElement& element, bool all) override;
116   virtual DomElementType domElementType() const override;
117   virtual void propagateRenderOk(bool deep) override;
118   virtual void setFormData(const FormData& formData) override;
119 };
120 
121 }
122 
123 #endif  // WT_WCOLORPICKER_H_
124