| 1 | #ifndef _GTKMM_STYLEPROPERTY_H |
| 2 | #define _GTKMM_STYLEPROPERTY_H |
| 3 | |
| 4 | /* Copyright (C) 2014 The gtkmm Development Team |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <gtkmm/widget.h> |
| 21 | #include <glibmm/ustring.h> |
| 22 | #include <glibmm/value.h> |
| 23 | |
| 24 | namespace Gtk |
| 25 | { |
| 26 | |
| 27 | /** Base class for widget style properties. |
| 28 | * |
| 29 | * This class manages the value type-agnostic bits of style properties. |
| 30 | * |
| 31 | * @newin{3,16} |
| 32 | */ |
| 33 | class GTKMM_API StylePropertyBase |
| 34 | { |
| 35 | public: |
| 36 | |
| 37 | /** Returns the name of the style property. |
| 38 | */ |
| 39 | Glib::ustring get_name() const; |
| 40 | |
| 41 | protected: |
| 42 | Gtk::Widget* widget_; |
| 43 | GType value_type_; |
| 44 | GParamSpec* param_spec_; |
| 45 | |
| 46 | /** Constructs a style property of type @a value_type for @a widget. |
| 47 | * The property is not registered in Gtk, call install_style_property() |
| 48 | * in order to do that. The properties are usually installed on the |
| 49 | * initialization of the first instance of a widget. |
| 50 | */ |
| 51 | StylePropertyBase(Gtk::Widget& widget, GType value_type); |
| 52 | ~StylePropertyBase() noexcept; |
| 53 | |
| 54 | /** Checks if the style property has already been installed. |
| 55 | */ |
| 56 | bool lookup_style_property(const Glib::ustring& name); |
| 57 | |
| 58 | /** Installs the style property specified by the given @a param_spec. |
| 59 | */ |
| 60 | void install_style_property(GParamSpec* param_spec); |
| 61 | |
| 62 | /** Returns the name of the style property. |
| 63 | */ |
| 64 | const char* get_name_internal() const; |
| 65 | |
| 66 | private: |
| 67 | // noncopyable |
| 68 | StylePropertyBase(const StylePropertyBase&); |
| 69 | StylePropertyBase& operator=(const StylePropertyBase&); |
| 70 | }; |
| 71 | |
| 72 | /** Represents a widget style property. |
| 73 | * |
| 74 | * This class maps one to one to a style property of a widget in Gtk. A style |
| 75 | * property is similar to object properties (Glib::Property), but its value is |
| 76 | * stored in the style object associated with the widget. The style property, |
| 77 | * similarly to the object properties, has some global data for each property. |
| 78 | * The global data includes the following information about the style property: |
| 79 | * * unique name, used to identify the style property |
| 80 | * * human-readable nickname |
| 81 | * * short description |
| 82 | * * default value, minimum and maximum bounds (applicable depending on the |
| 83 | * type of the style property) |
| 84 | * * flags, defining, among other things, whether the property can be read or |
| 85 | * written |
| 86 | * |
| 87 | * The StyleProperty class currently supports only the name and default value. |
| 88 | * The minimum and maximum bounds are set to the full range of the value. The |
| 89 | * nickname and the description are set to empty. The flags are set to indicate that |
| 90 | * the property is read-only (this doesn't disallow a style provider to set the |
| 91 | * value of the property). |
| 92 | * |
| 93 | * The global information must be installed into the widget once per property. |
| 94 | * This is handled automatically. A reference to the widget must be passed on |
| 95 | * the construction of the style property. |
| 96 | * |
| 97 | * @newin{3,16} |
| 98 | */ |
| 99 | template <class T> |
| 100 | class StyleProperty : public StylePropertyBase |
| 101 | { |
| 102 | public: |
| 103 | typedef T PropertyType; |
| 104 | typedef Glib::Value<T> ValueType; |
| 105 | |
| 106 | /** Constructs a style property for @a widget with @a name. For each instance |
| 107 | * of the widget, the same property must be constructed with the same name. |
| 108 | */ |
| 109 | StyleProperty(Gtk::Widget& widget, const Glib::ustring& name); |
| 110 | |
| 111 | /** Constructs a style property for @a widget with @a name and @a default_value. |
| 112 | * For each instance of the widget, the same property must be constructed with |
| 113 | * the same name and default value. |
| 114 | */ |
| 115 | StyleProperty(Gtk::Widget& widget, const Glib::ustring& name, const PropertyType& default_value); |
| 116 | |
| 117 | /** Returns the value of the style property. Equivalent to |
| 118 | * Gtk::Widget::get_style_property(get_name(), PropertyType& value). |
| 119 | */ |
| 120 | inline PropertyType get_value() const; |
| 121 | |
| 122 | /** Returns the value of the style property. Equivalent to |
| 123 | * Gtk::Widget::get_style_property(get_name(), PropertyType& value). |
| 124 | */ |
| 125 | inline operator PropertyType() const; |
| 126 | }; |
| 127 | |
| 128 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 129 | |
| 130 | /**** Gtk::StyleProperty<T> ****************************************/ |
| 131 | |
| 132 | template <class T> |
| 133 | StyleProperty<T>::StyleProperty(Gtk::Widget& widget, const Glib::ustring& name) |
| 134 | : |
| 135 | StylePropertyBase(widget, ValueType::value_type()) |
| 136 | { |
| 137 | if (!lookup_style_property(name)) |
| 138 | { |
| 139 | ValueType value; |
| 140 | value.init(value_type_); |
| 141 | GParamSpec* param_spec = value.create_param_spec(name); |
| 142 | param_spec->flags = (GParamFlags)(param_spec->flags & ~G_PARAM_WRITABLE); // value.create_param_spec() sets read-write flags |
| 143 | install_style_property(param_spec); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | template <class T> |
| 148 | StyleProperty<T>::StyleProperty(Gtk::Widget& widget, const Glib::ustring& name, |
| 149 | const typename StyleProperty<T>::PropertyType& default_value) |
| 150 | : |
| 151 | StylePropertyBase(widget, ValueType::value_type()) |
| 152 | { |
| 153 | if (!lookup_style_property(name)) |
| 154 | { |
| 155 | ValueType value; |
| 156 | value.init(value_type_); |
| 157 | value.set(default_value); |
| 158 | GParamSpec* param_spec = value.create_param_spec(name); |
| 159 | param_spec->flags = (GParamFlags)(param_spec->flags & ~G_PARAM_WRITABLE); // value.create_param_spec() sets read-write flags |
| 160 | install_style_property(param_spec); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | template <class T> inline |
| 165 | typename StyleProperty<T>::PropertyType StyleProperty<T>::get_value() const |
| 166 | { |
| 167 | PropertyType value; |
| 168 | widget_->get_style_property(get_name(), value); |
| 169 | return value; |
| 170 | } |
| 171 | |
| 172 | template <class T> inline |
| 173 | StyleProperty<T>::operator PropertyType() const |
| 174 | { |
| 175 | return this->get_value(); |
| 176 | } |
| 177 | |
| 178 | #endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 179 | |
| 180 | } // namespace Gtk |
| 181 | |
| 182 | #endif /* _GTKMM_STYLEPROPERTY_H */ |